iOS bug修复ios专题iOS进阶指南

iOS 隐藏navigationBar带来的问题

2017-02-13  本文已影响289人  SunshineBrother

相信小伙伴们都遇到过隐藏navigationBar这样的需求,但是有一个问题让我蛋疼了好久都没有办法解决,最近项目闲下来,就研究了一下这个问题,让遇到这个问题的小伙伴们也有一个解决这个问题的思路。有更好想法的小伙伴,欢迎提取宝贵的建议。

遇到问题的效果图
tableView悬停.gif

亲们,仔细看最上边可以看出,在隐藏navigationBar的时候,当我们创建一个tableView
的时候,当tableView向下拉的时候,最上面会出现一片空白(为了让小伙伴们能够看得跟清晰,我把背景色设置为灰色),显示效果非常不好看。以前,我每次遇到这个问题的时候,都是头疼不已,想各种办法解决。功夫不负有心人,最近想到了一个办法解决

改正后的效果图
tableView悬停.gif
解决思路:

1、设置一个tableView ,他的大小跟视图控制器小一样
2、给tableView设置一个头视图(或者设置内边距)
3、�设置一个ImageView,imageView大小跟头视图一般大,在tableView上下拉的时候,在代理方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView中重新设置imageView的大小
4、当tableView向上滚动一定距离的时候,我们可以自定制一个navigationItem,让他慢慢显示

注意点

1、设置一个tableView ,他的大小跟视图控制器小一样

_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor grayColor];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];

2、给tableView设置一个头视图(或者设置内边距)

 //tableView的头部视图和尾部视图
    UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    _tableView.tableHeaderView = tableHeaderView;

_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    _imageView.image = [UIImage imageNamed:@"image"];
    [self.view addSubview:_imageView];

3、设置导航栏标题,在刚刚创建时设置view 的alpha=0

 UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    _redView = view;
    view.alpha = 0;
    
    UILabel* titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    titleLabel.text = @"我是title";
    titleLabel.textColor  = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleLabel];

4、重点代码,在代理方法中设置各个空间的frame

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY < 0) {
        _imageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
        CGRect rect = _imageView.frame;

        CGFloat rate = rect.size.width / rect.size.height;
        //下拉的时候当前的高度
        float height =  - offsetY + 200;
        rect.size.height = height;
        //下拉是应该出现的宽度
        float width = height * rate;
        rect.size.width = width;
        _imageView.frame = rect;
        
//这个代码是设置中心放大
        CGPoint center = _imageView.center;
        center.x = SCREEN_WIDTH/2;
        center.y = 100 - offsetY/2;
        _imageView.center = center;
        
        
        _redView.alpha = 0;
        
    }else{
        CGRect rect = _imageView.frame;
        rect.origin.y = -offsetY;
        _imageView.frame = rect;
//因为下拉的过程offsetY 不是一次+1 的增加的,所以判断当offsetY>64时设置_redView.alpha = 1.0
        if (offsetY <= 64) {
           float MYApla = 1.0/64 * offsetY;
            _redView.alpha = MYApla;
        }else{
            _redView.alpha = 1.0;
        }
    }

}


上一篇下一篇

猜你喜欢

热点阅读