经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
iOS状态栏、导航栏的一些笔记分享
来源:jb51  时间:2019/4/8 12:18:45  对本文有异议

前言

IOS的界面分为状态栏和导航栏,如下图所示:

状态栏与导航栏的位置如上图,我们可以通过[UIApplication sharedApplication].statusBarFrame.size获取状态栏的size(一般没有刘海时的高度为20,有刘海时的高度为44)。

通过self.navigationController.navigationBar.frame.size获取导航栏的size(一般高度为44,大标题时高度为xyz,当然也可以通过自定义来改变导航栏样式)。

***ps:***在我们通过[nav.navigationBar setBarTintColor:[UIColor lightGrayColor]];来设置导航栏颜色时,将导致导航栏和状态栏背景色均变为浅灰色。

1. 状态栏内容是否高亮

状态栏内容包括信号、时间、电量等,只有两种颜色样式(黑或白)。iOS开发过程中提供修改状态栏内容颜色样式的方法:

在代码中设置状态栏内容颜色:info.plist文件中直接设置状态栏内容颜色:在info.plist中添加字段View controller-based status bar appearance, 当其取值为YES时,表示以UIController对状态栏的设置为准,UIApplication对状态栏进行的设置将不起作用。

  1. // 在controller中重写该方法,并返回相应值
  2. - (UIStatusBarStyle)preferredStatusBarStyle {
  3. return UIStatusBarStyleLightContent;
  4. }
  5.  
  6. // 注意:
  7. // 当controller嵌入UINavigationController中时,controller中的方法preferredStatusBarStyle是不会自动被调用的,而navController中的该方法会被调用;
  8. // 当有navController时,在重写controller中的preferredStatusBarStyle方法同时,我们还应重写navController中的childViewControllerForStatusBarStyle方法。
  9. - (UIViewController *)childViewControllerForStatusBarStyle {
  10. return self.topViewController;
  11. }

当字段View controller-based status bar appearance取值为NO时,则以UIApplication为准,控制器设置状态栏的方法preferredStatusBarStyle则根本不会被调用。

  1. // 状态栏内容-黑色
  2. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  3. // 状态栏内容-白色
  4. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

在info.plist文件中直接设置状态栏内容颜色:字段View controller-based status bar appearance取值为NO,且Status bar style取值为UIStatusBarStyleLightContent(可选),则不写代码,也可控制应用中的状态栏内容颜色。

2. 隐藏状态栏

整个项目隐藏在Targets -> General -> 勾选 Hide status bar 即可。

在单个界面中隐藏

  1. // iOS 9.0 之前
  2. // 隐藏=YES,显示=NO; Animation:动画效果
  3. [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
  4.  
  5. // iOS 9.0 之后推荐使用这个
  6. // 注意:该方法可以通过UIController中的方法setNeedsStatusBarAppearanceUpdate来间接调用
  7. - (BOOL)prefersStatusBarHidden {
  8. return YES;
  9. }

注意:上面两个操作状态方法依然受到Info.plist中字段View controller-based status bar appearance取值得影响,该字段取值为YES时,进入controller会自动调用prefersStatusBarHidden方法。当controller嵌入UINavigationController中时,controller中的方法prefersStatusBarHidden是不会自动被调用的,而navController中的该方法会被调用;当有navController时,在重写controller中的prefersStatusBarHidden方法同时,我们还应重写navController中的childViewControllerForStatusBarHidden方法。

  1. - (UIViewController *)childViewControllerForStatusBarHidden {
  2. return self.topViewController;
  3. }

启动页隐藏状态栏,进入程序后正常显示状态栏
首先,在Targets->General->勾选中Hide status bar或者在info.plist里面 Status bar is initially hidden 设置为 YES;
其次,在AppDelegate.m中添加代码

  1. -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. [application setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  4. }

3. 导航栏


UINavigationController的视图层次结构比较清晰,用户可见的导航栏即为其中的***UINavigationBar***,通过它,我们就可以修改导航栏的样式。下面我们就逐步介绍一些常用的关于导航栏的操作。

导航栏常用操作

  1. // 隐藏导航栏
  2. //[self.navigationController setNavigationBarHidden:YES];
  3. [self.navigationController setNavigationBarHidden:YES animated:YES];
  4.  
  5. // 设置导航背景为红色
  6. [self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];
  7.  
  8. // 设置navigationBar的透明效果
  9. [self.navigationController.navigationBar setTranslucent:YES];
  10. //设置导航栏的背景图片
  11. [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"imgName"] forBarMetrics:UIBarMetricsDefault];
  12.  
  13. // Attributes 属性
  14. NSDictionary *textAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:30]};
  15. // 设置导航栏标题的字体大小、颜色
  16. [self.navigationController.navigationBar setTitleTextAttributes:textAttributes];

4. 导航栏常用样式

在日常开发中,我们经常需要修改导航栏样式,如使导航栏透明、导航栏尺寸等,在不同样式导航栏之间的切换时还要注意是否平顺...(隐藏,与正常导航栏的切换)

导航栏大标题

  1. if (@available(iOS 11.0, *)) {
  2. [self.navigationController.navigationBar setPrefersLargeTitles:YES];
  3. }

自定义导航栏大标题样式

重写UINavigationBar中的layoutSubviews方法,可通过发送通知的形式来监听导航栏高度变化,如下:

  1. -(void)layoutSubviews {
  2. [super layoutSubviews];
  3. [[NSNotificationCenter defaultCenter] postNotificationName:KEY_UINavigationBar_Height_Changed object:self userInfo:nil];
  4. }

使导航栏透明

当需要设置导航栏透明且title等项正常显示时,最好将设置过程置于viewWillAppear:方法中:

  1. //// 需要导航栏透明的ViewController中
  2. - (void)viewWillAppear:(BOOL)animated {
  3. [super viewWillAppear:animated];
  4. _navView.hidden = NO;
  5. self.edgesForExtendedLayout = UIRectEdgeTop;
  6. self.navigationController.navigationBar.translucent = YES;
  7. [self.navigationController.navigationBar setShadowImage:[UIImage new]];
  8. [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
  9. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
  10. }
  11.  
  12. //// 导航栏为非透明的ViewController中
  13. - (void)viewWillAppear:(BOOL)animated {
  14. [super viewWillAppear:animated];
  15. self.edgesForExtendedLayout = UIRectEdgeNone;
  16. self.navigationController.navigationBar.translucent = NO;
  17. [self.navigationController.navigationBar setShadowImage:nil];
  18. [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
  19. [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
  20. [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]}];
  21. }

关于自定义导航栏、tabBar的例子将在后续文章中介绍...

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号