self.navigationItem.title = @"标题";
复制代码
正常状况下,控制器的标题会默认做为导航标题bash
self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
复制代码
//注意上层有毛玻璃遮挡
self.navigationController.navigationBar.backgroundColor = [UIColor blueColor];
复制代码
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Background"] forBarMetrics:UIBarMetricsDefault];
复制代码
在iOS7以前微信
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
复制代码
iOS7以后app
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
复制代码
若iOS7以后仍要使用第一种方法全局设置,则须要在plist文件中添加View controller-based status bar appearance
字段,值为NO
,意为不使用控制器管理状态栏。ide
//只设置颜色
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
复制代码
//设置成图片
UIImage *leftButtonImage = [[UIImage imageNamed:@"image"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftButtonImage
style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
复制代码
//设置成文字
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlain target:self action:@selector(back)];
复制代码
//自定义视图
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
复制代码
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
// 控制手势在根控制器时不触发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return self.childViewControllers.count > 1;
}
复制代码
//设置透明的背景图,便于识别底部线条有没有被隐藏
[navigationBar setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
//此处使底部线条透明
[navigationBar setShadowImage:[UIImage new]];
复制代码
另外能够经过颜色转图片来修改导航条底部分隔线颜色字体
//动态地改变UIColor的alpha属性能够返回,不一样alpha的图片;可用于动态改变导航条的透明度
+ (UIImage *)imageWithColor:(UIColor *)color{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
复制代码
//全局设置导航栏主题,只在AppDelegate中有效,
或者是UINavagaitonController中的RootController 中设置有效
- (void)setNavigationControllerAppearance {
[UINavigationBar appearance].barStyle = UIBarStyleBlack;
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithWhite:0.1 alpha:0.5]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
//也能够设置成图片
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
复制代码
//方法二
UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
leftButton.backgroundColor = [UIColor clearColor];
leftButton.frame = CGRectMake(0, 0, 45, 40);
[leftButton setImage:[UIImage imageNamed:@"LeftButton_back_Icon"] forState:UIControlStateNormal];
[leftButton setTitle:@"返回" forState:UIControlStateNormal];
leftButton.tintColor = [UIColor whiteColor];
leftButton.autoresizesSubviews = YES;
leftButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
leftButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
[leftButton addTarget:self action:@selector(goToBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItems = @[backItem,closeItem];
复制代码
self.navigationController.navigationBar.hidden = YES;
复制代码
if (scrollView.contentOffset.y > 64) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}else{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
复制代码
注意:两种方法都是能够隐藏导航栏的,隐藏以后依然能够使用push和pop方法。可是若是用navigationBar.hidden隐藏导航栏,咱们能够继续使用navigationBarHidden提供的滑动pop效果,若是用navigationBarHidden,这个操做将无效;但前者navigationBar.hidden没有系统自动的动画效果。动画
//不让其自动调整
self.automaticallyAdjustsScrollViewInsets = NO;
复制代码
笔者和朋友作了一个小副业,微信公众号,替你省钱,分享还能赚点小钱; 帮忙关注,支持一下,权当请我喝咖啡,谢谢。 若是很差用,能够取消。 ui