笔记技术文档iOS Developer

导航条重点注意集锦

2016-02-24  本文已影响231人  Z了个L
    //从ios7开始当scrollView在导航控制器,会自动调用边距64
    self.automaticallyAdjustsScrollViewInsets = NO;
    //调用 contentInset时会调用scrollViewDidScroll
    self.tableView.contentInset = UIEdgeInsetsMake(244, 0, 0, 0);
// 导航条和导航条里面的子控件设置透明度是没有效果的.
// 下面两种方式隐藏导航条是不可行的
//  self.navigationController.navigationBar.hidden = YES;
//  self.navigationController.navigationBar.alpha = 0;
//调用 contentInset时会调用scrollViewDidScroll
self.tableView.contentInset = UIEdgeInsetsMake(244, 0, 0, 0);
// 设置背景图片,必须得要UIBarMetricsDefault
    // 如果指定图片传为nil,会默认给设置一张默认图片,也不行
    // 隐藏导航条分2步
    // 第1步,隐藏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    // 第2步,隐藏剩下那条线,通过隐藏阴影图片
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];

    UILabel *titleT = [[UILabel alloc] init];
    titleT.text = @"个人详情页";
    [titleT sizeToFit];
    // 要隐藏titleT通过设置alpha是没有效果的,得通过设置颜色的透明度
    //    titleT.alpha = 0; 没有效果
    //设置文字的颜色为透明
    [titleT setTextColor:[UIColor colorWithWhite:0 alpha:0]];
    self.navigationItem.titleView = titleT;

    UILabel *titleT = (UILabel *)self.navigationItem.titleView;
    [titleT setTextColor:[UIColor colorWithWhite:0 alpha:alpha]];
#pragma mark - UIScrollViewDelegate方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y - oriY; // tableView的偏移量

    // 图片剩下的高度
    CGFloat h = oriH - offsetY;
    if (h <= 64) { // 如果剩下的高度 <= 64
        h = 64;
    }
    // 改变约束值
    self.heightCons.constant = h;

    // 根据透明度传入图片
    // 根据一个颜色生成一张图片
    // 方法总结:类似的问题:找两个最大值,放 / 两端,比如alpha最大值为1,
    // 偏移量最大为136,放两端,剩下的变量与前面的数相乘,ok,
    CGFloat alpha = 1 * offsetY / 136;
    if (alpha >= 1) {
        alpha = 0.99;
    }
    // 根据颜色,生成一张图片
    UIImage *image = [UIImage imageWithColor:[UIColor colorWithWhite:1 alpha:alpha]];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

    UILabel *titleT = (UILabel *)self.navigationItem.titleView;
    [titleT setTextColor:[UIColor colorWithWhite:0 alpha:alpha]];

}

上一篇 下一篇

猜你喜欢

热点阅读