设置导航栏标题
self.navigationItem.title = @"导航栏标题"
设置导航栏的背景颜色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
设置导航栏的背景图片
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"图片"] forBarMetrics:UIBarMetricsDefault];
UIBarMetrics枚举:
UIBarMetricsDefault -> 在横竖屏都显示
UIBarMetricsCompact -> 只有横屏才显示
...
更改顶部状态栏的颜色
系统提供两种样式:
- UIStatusBarStyleDefault:系统默认样式,黑色内容,用于浅色的背景
- UIStatusBarStyleLightContent:白色内容,用于深色的背景
- 需要在工程的Info.plist文件中添加一行View controller-based status bar appearance,设置为NO
- 然后在ViewController中添加如下方法
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
设置返回按钮
- 设置返回按钮的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
- 设置返回按钮图片
// imageWithRenderingModel:方法,参数UIImageRenderingModeAlwaysOriginal表示总是用原图渲染,如果不这么设置,返回的按钮将会显示tintColor的颜色,UITabbarItem存在同样问题
UIImage *image = [[UIImage imageName:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:style:UIBarButtonItemStyleBordered target:self action:@selector(backMethod)];
self.navigationItem.leftBarButtonItem = leftButton;
// 修复navigationController侧滑关闭失效问题
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
- 设置返回按钮文字
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(backMethod)];
letfButton.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = leftButton;
- 自定义返回按钮
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButtonView];
self.navigationItem.leftBarButtonItem = leftBarButton;
rightBarButton 参照如上
隐藏导航栏底部的线条
- 设置导航栏的shadowImage
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setShadowImage:[UIImage new]]
- 设置导航栏的clipsToBounds的值
self.navigationController.navigationbar.clipsToBounds = YES;
设置导航栏添加多个按钮
- 方法一
UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
self.navigationItem.leftItemSupplementBackButton = YES;
- 方法二
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
...自定义button过程
UIBarButton *backItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItems = @[backItem, closeItem];
导航栏全局属性设置
[UINavigationBar appearance].barStyle = UIBarStyleBlack;
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];