iOS Developer

IOS 导航栏、状态栏的设置

2017-01-16  本文已影响573人  欲码还修

在iOS开发过程中,导航栏和状态栏的使用有着毋庸置疑的重要性。尤其遇到一些不怎么会设计的***时候。所以我们必须对一些常用属性有较深的理解才能应对自如。下面是我从项目中总结的一些导航栏和状态栏的使用方法:

一、导航栏的设置 UINavigationBar

先上两张官方文档里面的图片,各个属性描述的很详细。


导航栏Item.png
导航栏属性一览表.png

1. 导航栏外观(UINavigationBar)设置

  //导航栏的背景色设置
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
  //坐标起始位置设置
[[UINavigationBar appearance] setTranslucent:NO]; //(0.64)开始
  // 创建字典保存 backItem (字体大小,颜色...)设置
NSMutableDictionary *atts = [NSMutableDictionary dictionary];
atts[NSFontAttributeName] = [UIFont systemFontOfSize:13.0];
atts[NSForegroundColorAttributeName] = [UIColor redColor];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearance] setTitleTextAttributes:atts forState:UIControlStateNormal];
  //创建字典保存topItem(字体大小,颜色...)设置
NSMutableDictionary *topItemDic = [NSMutableDictionary dictionary];
topItemDic[NSFontAttributeName] = [UIFont systemFontOfSize:18.0];
topItemDic[NSForegroundColorAttributeName] = [UIColor colorWithRed:255/255.0 green:137/255.0 blue:51/255.0 alpha:1];;
[[UINavigationBar appearance] setTitleTextAttributes:topItemDic];
[navi.navigationBar setTitleTextAttributes:topItemDic];
  //设置导航栏backItem图片
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"backIcon"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backIcon"]];

2.UINavigationItem设置

因为系统默认leftBarButtonItem距离左边距离为20,而很多需求都要求小于20。使用以下方法可以实现:

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[btn setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
btn.tintColor = [UIColor cyanColor];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//设置 leftBarButtonItem 向左边靠近10个点
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, backNavigationItem];
}else{
        self.navigationItem.leftBarButtonItem = backNavigationItem;
}

2.透明导航栏

不透明导航栏.png

其实很简单,将导航栏translucent属性设置为YES,然后放一张透明图片即可。

self.navigationController.navigationBar.translucent = YES;  //坐标从(0,0)开始 
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"透明图片.png"] forBarMetrics:UIBarMetricsDefault]; 
self.navigationController.navigationBar.clipsToBounds = YES;//去掉白线

此时,导航栏就变成透明的了。


透明导航栏.png

有时,我们会为了界面的坐标从(0,64)开始,所以会将translucent属性设置为NO,所以退出该界面时会导致其他界面出现问题。还有,将导航栏设置透明之后,push出其他界面是导航栏也会有问题,此时,我们需要在viewWillDisappear中将navigationBar的背景图片取消:

self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.clipsToBounds = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""]forBarMetrics:UIBarMetricsDefault];

二、状态栏的设置 UIStatusBarStyle

状态栏,会因为info.plist中View controller-based status bar appearance 的两种状态(YES or NO)需要两种不同方式进行设置
以下是官方文档上介绍的 状态栏的样式信息:


状态栏样式.png

// default 黑色的前景,用来放在亮色背景上;
// lightContent 白色的前景,用来放在暗色背景上;

View controller-based status bar appearance 设置为YES时,状态栏的设置方式:

// 状态栏的隐藏/显示
- (BOOL) prefersStatusBarHidden{
    return YES; //隐藏状态栏
}
//状态栏的背景色 UIBarStyleDefault->黑色
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack]; //白色

View controller-based status bar appearance 设置为NO时,状态栏的设置方式需要使用UIApplication :

// 隐藏/显示 状态栏
[UIApplication sharedApplication].statusBarHidden = YES; 
//状态栏的背景色 UIStatusBarStyleDefault->黑色
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

后续持续补充.....

上一篇下一篇

猜你喜欢

热点阅读