ios 对navigation的终极方案

由于项目需要最近多navigation的操作颇多,为了便于以后使用也方便大家,所以在这里一一记录下来(以375*667为例)


</br>

1.设置navigationBar透明(系统默认)

代码:

[self.navigationController.navigationBar setTranslucent:YES];

效果:可以使得navigationBar呈现半透明,类似UIVisualEffectView的效果,并且可以增加tableView的滑动区域(注意是滑动区域而不是滚动区域)。意思是
可以设置

self.tableView= [[UITableViewalloc]initWithFrame:self.view.bounds];

实际效果为tableview的实际显示区域只有 667 - 64;但是他的有效滚动区域却是667

/*

New behavior on iOS 7.

Default is YES.

You may force an opaque background by setting the property to NO.
If the navigation bar has a custom background image, the default is inferred
from the alpha values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a bar with an opaque custom background image
it will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar's barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
>*/

根据上面说法,当设置背景图的时候,只要背景图的alpha<1 ,那么translucent就会被设置为YES。


</br>

2.关于automaticallyAdjustsScrollViewInsets

代码:

self.automaticallyAdjustsScrollViewInsets=YES;

效果:在有navigationBar的状态下,scrollView会默认被下推64px,就是因为默认设置self.automaticallyAdjustsScrollViewInsets=YES 。 这样的状态在navigationBar是透明的情况下是可以设置为NO并且有效的,但是在translucent = NO的情况下不能改变。


</br>

3.设置navigationBar全透明

1.用rgbColor创建一张透明的图片设置为背景图

<pre><code>
[self.navigationController.navigationBarsetBackgroundImage:[UIImagecreateImageWithColor:[UIColorclearColor]]forBarMetrics:UIBarMetricsDefault];
</code></pre>

2.找到并设置navigationBar的imageView线隐藏


- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


</br>

4.滚动tableView、scrollView,让navigationbar的隐藏的2种方法

方法一

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    
    if (scrollView == self.containerView) {
        if(velocity.y>0)
            
        {
            [self.navigationController setNavigationBarHidden:YES animated:YES];
        }
        
        else
        {
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
    }
}

方法二

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //<0的话是正常
    
    if (_tableView.contentOffset.y <= -64) {//显示
        self.navigationController.navigationBar.frame = CGRectMake(0, 20, 375, 44);
        [self.navigationController setNavigationBarHidden:NO animated:NO];
    }
    
    else if (_tableView.contentOffset.y < 0){
        self.navigationController.navigationBar.frame = CGRectMake(0, 20 - (_tableView.contentOffset.y + 64) , 375, 44);
    }else{
        self.navigationController.navigationBar.frame = CGRectMake(0, -44, 375, 44);
        [self.navigationController setNavigationBarHidden:YES animated:NO];
    }
    
    //-64 是正常的contentoff
}


</br>

5.navigationbar的颜色渐变

颜色渐变也可以参考navigationbar隐藏的方案二方法,实现起来还是比较方便的


</br>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 总所周知,苹果从iOS7开始采用扁平化的界面风格,颠覆了果粉们“迷恋”的拟物化风格。对于开发者而言,全新的风格带来...
    聪明的笨白阅读 3,544评论 8 40
  • 先声明:以下总结只对ios7及ios7之后才有效~~~ 之前开发过程中偶尔会遇到设置导航栏透明与否或者运行系统版本...
    Qiu_W阅读 1,430评论 0 1
  • 母亲这一辈的人,她们经常在网络上笨拙地摸索,各种乱七八糟的图标、隐蔽的交互动作和令人混淆的界面时常让她们不知该怎么...
    喵癌晚期阅读 3,558评论 23 87
  • 幸会,妈妈 张春 我妈年轻的时候是一名会计,在食品站工作。那个年代的屠夫看不起坐办公室的,男人看不起女人,双重歧视...
    我心怡然阅读 1,060评论 1 1
  • 一坐在办公室的电脑前到中午左右就要头疼了,看来是不能这么努力的盯着屏幕去设计,哪不舒服已经很少说了,能自己解决的就...
    狗蛋君阅读 235评论 0 0