这个问题是这篇所谓问题中我花费时间最长才解决的,对于初学者的我真是一个不小的坑。
先说一下走过的弯路,一开始是经过继承UIViewController(好比叫MyViewController),并设置其navigationItem
的属性leftBarButtonItem
来实现的,并且必需要给这个按钮实现点击就返回的方法。
这样作的缺点有:web
全部放在 UINavigationController 中的 ViewController 都要继承了这个 MyViewControllerapp
若是是在 UINavigationController 的栈底的 ViewController,须要增长一个隐藏返回按钮的判断字体
若是从 MyViewController 及其子类 push 了一个 iOS 的原生界面,好比 UIImagePickerController,那么这个返回按钮就失效了spa
可是,终于让我找到了一个更简单而且解决以上全部缺点的方法,在 AppDelegate 中进行全局设置,代码以下:设计
UIImage *backImage = [[[UIImage imageNamed:@"navigation_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, 11.5, 0)]; [[UINavigationBar appearance] setBackIndicatorImage:backImage]; [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:backImage];
注:返回按钮的图片我采用的是44*44 point 的图片,可是不知道为何若是直接设置就会偏上11.5 point,只好校订一下。code
另外,若是想把返回按钮的文字隐藏,我只找到了这么一个 workaround 的奇技淫巧:orm
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
其实就是把按钮文字向上移了60 point,并无隐藏,只是在屏幕上看不到而已,用 Reveal 仍是能够看到……继承
[UINavigationBar appearance].barTintColor = [UIColor blueColor];
NavigationBar 上面有两处能够改变字体颜色,一是标题,二是左右按钮的文字。图片
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
iOS 7 NavigationBar的下方默认是有一条阴影的,若是想要 NavigationBar 和下面内容的背景颜色融为一体的话,就要去掉这个阴影:ci
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [[UINavigationBar appearance] setShadowImage:[UIImage new]];
[UITabBarItem.appearance setTitleTextAttributes: @{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal]; [UITabBarItem.appearance setTitleTextAttributes: @{ NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];
iOS7之后,status bar 的背景颜色变成了透明色,并且系统会根据 app的颜色自动改变 status bar 的字体颜色(黑和白)。可是这个自动改变的字体颜色并不必定和全部的 app 都搭配,好比咱们 app 的主题色是稍微浅一丢丢的蓝,可是系统匹配的 status bar 的字体颜色就是黑色,看起来就很不爽,因此就要强制将其改成白色。
首先在 Info.plist 中的 Information Property List 中添加一个 Key为View controller-based status bar appearance
的 item,其 Type 设为 Boolean,Value 设为 NO
而后在AppDelegate.m
的application:didFinishLaunchingWithOptions:
中添加:[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
有时候为了实现沉浸式设计,好比 app 首次打开的引导页,须要隐藏整个 StatusBar,方法以下:
和改变 StatusBar 颜色同样,在 Info.plist 中的 Information Property List 中添加一个 Key为View controller-based status bar appearance
的 item,其 Type 设为 Boolean,Value 设为 NO
在须要隐藏StatusBar 的 ViewController 中的viewDidLoad
加入如下代码:
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [self prefersStatusBarHidden]; [self setNeedsStatusBarAppearanceUpdate]; }
重写prefersStatusBarHidden
:
-(BOOL)prefersStatusBarHidden { return YES; }
注:可是这样设置的话从这个页面开始整个 app 的 StatusBar 都会隐藏,若是想要再显示出来,只须要在其余 ViewController 中加入:
[UIApplication sharedApplication].statusBarHidden = NO;