2018-07-19关于导航栏

通过UINavigationController组织视图层级时,导航栏是个不大不小的坑。

navigationBar有个属性translucent,透明度或模糊度。ios 7之后默认为YES。

在navigationController push操作的时候,要注意视图组件的坐标。

当translucent的值为YES时,视图的坐标起始位置时从(0,0)开始的,这就会造成导航栏会盖住视图的一部分,肯定不是我们想要的。

当translucent的值为NO时,视图的坐标起始位置时从(0,64)开始的,一切看起来是正常的。

不管你push的视图是滚动视图还是非滚动视图。

当然,还有一个方法是可以的

self.edgesForExtendedLayout=UIRectEdgeNone;

如果页面较多的时候,可以通过分类或者继承,减少代码量。


#import "UIViewController+Nav.h"

@implementationUIViewController (Nav)

-(void)addNavBar{

    self.edgesForExtendedLayout=UIRectEdgeNone;

    UIBarButtonItem*leftBar=[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(gotoBack)];

    self.navigationItem.leftBarButtonItem=leftBar;

}

-(void)gotoBack{

    [self.navigationController popViewControllerAnimated:self];

}


隐藏导航栏底部横条的小技巧:

1、单个页面

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];

    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setShadowImage:nil];

}

2、全局页面

[[UINavigationBarappearance] setBackgroundImage:[[UIImagealloc] init] forBarMetrics:UIBarMetricsDefault];

[[UINavigationBarappearance] setShadowImage:[[UIImagealloc] init]];

3、

self.navigationController.navigationBar.clipsToBounds =YES;

4、设置导航栏底部线条颜色

UINavigationBar*navigationBar =self.navigationController.navigationBar;

 [navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAnybarMetrics:UIBarMetricsDefault];

[navigationBar setShadowImage:[UIImage  imageWithColor:[UIColorredColor]]];


+ (UIImage*)imageWithColor:(UIColor*)color{

CGRectrect =CGRectMake(0.0f,0.0f,1.0f,1.0f);UIGraphicsBeginImageContext(rect.size);

CGContextRefcontext =UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [colorCGColor]);

CGContextFillRect(context, rect);

UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();returnimage;}

你可能感兴趣的:(2018-07-19关于导航栏)