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

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

UINavigationBar设置

1.设置背景颜色:

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

效果如下:

 

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

1
[ self .navigationController.navigationBar setTranslucent: YES ];  

设置为NO的效果:

 

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

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

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

1
2
3
NSDictionary   *textAttributes=@{ NSForegroundColorAttributeName :[UIColor whiteColor], NSFontAttributeName :[UIFont systemFontOfSize:20]};
 
[ self .navigationController.navigationBar setTitleTextAttributes:textAttributes];

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

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

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

单个leftItem设置:

1
2
3
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设置:

1
2
3
4
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的覆写方法:

1
[ self .navigationController.navigationBar setBarStyle:UIBarStyleBlack];

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

1
2
3
4
- (UIStatusBarStyle)preferredStatusBarStyle
{
     return  UIStatusBarStyleLightContent;
}

效果如下:

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

1
2
3
4
- ( BOOL )prefersStatusBarHidden
{
     return  YES ;
}

效果如下:

如果需要重新刷新一下状态栏,可以通过setNeedsStatusBarAppearanceUpdate控制~

本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4631384.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值