您的位置:首页 > 产品设计 > UI/UE

iOS开发-UINavigationBar和Status Bar实用技巧

2015-07-08 23:19 543 查看
iOS7之后关于UINavigationBar和Status Bar都发生了一系列的改变,如果不需要兼容iOS7之后的设备,按照网上有些资料去解决问题会踩到一些坑。在iOS 7中,我们可以修改每个View Controller中状态栏的外观,而iOS7之前的状态栏都是白色风格。iOS7之前设置背景颜色还需要搞个背景View覆盖,而iOS7只需要设置一下barTintColor颜色即可。

UINavigationBar设置

1.设置背景颜色:

[self.navigationController.navigationBar setBarTintColor:[UIColor redColor]];


效果如下:



2.Translucent设置成透明度,设置成YES会有一种模糊效果:

[self.navigationController.navigationBar setTranslucent:YES];  




设置为NO的效果:



3.设置背景图片,图片样式需要根据情况适用,调用方法为setBackGroundImage:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"FlyElephant.png"] forBarMetrics:UIBarMetricsDefault];


4.设置NavigationBar标题大小及颜色,之前设置是通过UITextAttributeFont,UITextAttributeTextColor,UITextAttributeTextShadowColor和UITextAttributeTextShadowOffset设置,现在需要都根据NS开头的属性去设置:

NSDictionary  *textAttributes=@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};

[self.navigationController.navigationBar setTitleTextAttributes:textAttributes];


5.NavigationBar设置中间的标题或者自定义View:

[self.navigationItem setTitle:@"旅行"];
[self.navigationItem setTitleView:[UIImage imageNamed:@"FlyElephant"];


6.单个或多个左右Item设置:

单个leftItem设置:

UIBarButtonItem  *leftBarButton=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"BackIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(popBack)];
[leftBarButton setTintColor:[UIColor colorWithWhite:0 alpha:1]];
self.navigationItem.leftBarButtonItem=leftBarButton;


效果如下:



多个rightItem设置:

UIBarButtonItem *firstItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:nil];
UIBarButtonItem *secondItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:nil];
NSArray *rightItems=@[firstItem, secondItem];
self.navigationItem.rightBarButtonItems=rightItems;


效果如下:



状态栏(StatusBar)设置

iOS7之前状态栏永远是白色风格,我们没有太多的空间去进行修改,iOS 7之后,我们可以修改每个控制器中状态栏的外观,比如说在白天和夜间模式的切换过程中我们可以修改状态栏的颜色。状态栏上的时间、电池指示器和Wi-Fi信号默认是暗色,如上图所示,不过我们可以通过修改UIBarStyle然后执preferredStatusBarStyle的覆写方法:

[self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];


最终执行的是覆写控制器中preferredStatusBarStyle的方法:(如果上面的代码不写是不会执行到preferredStatusBarStyle方法的)

- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}


效果如下:



隐藏状态栏只需要覆写一下prefersStatusBarHidden即可:

- (BOOL)prefersStatusBarHidden
{
return YES;
}


效果如下:



如果需要重新刷新一下状态栏,可以通过setNeedsStatusBarAppearanceUpdate控制~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: