iOS状态栏的设置有两种方式
这两种方式是根据UIViewControllerBasedStatusBarAppearance也就是View controller-based status bar appearance的值的不同有相应的设置
如果不添加UIViewControllerBasedStatusBarAppearance配置,那么默认值为YES
UIViewControllerBasedStatusBarAppearance设置为YES时
ViewController的配置优先级高于Application的配置
UIViewControllerBasedStatusBarAppearance设置为NO时
那么只取Application的配置,ViewController的配置不生效
也可以在Info.plist添加默认的状态栏样式
Status bar style设置为UIStatusBarStyleLightContent或UIStatusBarStyleDefault
项目的Info.plist添加配置View controller-based status bar appearance设置为NO
设置前景色为白色
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false);设置前景色为黑色
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: false);设置隐藏
动画有三种方式(Slide(滑动)/Fade(淡入淡出)/None(无动画))
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Slide)项目的Info.plist添加配置View controller-based status bar appearance设置为YES
重写以下方法
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}
override func prefersStatusBarHidden() -> Bool {
return false;
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Slide
}ViewController加载时会自动调用preferredStatusBarStyle方法和prefersStatusBarHidden方法
如果要想之后事件里设置前景色或隐藏的话,可以把样式和是否隐藏设置为全局变量,修改变量后直接调用
self.setNeedsStatusBarAppearanceUpdate()但是发现上面的动画配置不生效,按下面的方式调用就可以了
UIView.animateWithDuration(0.3) {
self.setNeedsStatusBarAppearanceUpdate()
}但这种配置有个弊端 就是
如果你的ViewController在NavigationController中,那么直接在ViewController设置是不生效的,只能在NavigationController中设置
所以如果你用的Storyboard中配置的NavigationController话,就必须为NavigationController添加一个自定义的类,就稍显麻烦了,所以并不推荐用这种方式设置
改变方法有两种
navigationBar的setBarTintColor接口,用此接口也会改变statusBar的背景色
注意:一旦你设置了navigationBar的- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics接口
那么上面的setBarTintColor接口就不能改变statusBar的背景色,statusBar的背景色就会变成纯黑色。
UIViewUIView的frame.size 和statusBar大小一样UIView的frame.origin 为{0,-20}UIView的背景色为你希望的statusBar的颜色navigationBar上addSubView该UIView即可上面说了状态栏 这里顺便把UINavigationBar也说了
我的配置
let appear = UINavigationBar.appearance();
//在早期版本上设置 不能全局的方式设置 会导致app崩溃
//appear.translucent = false;
//设置bar的颜色
appear.barTintColor = UIColor(red: 52/255, green: 146/255, blue: 233/255, alpha: 1.0);
//设置背景色(不透明时没用,因为barTintColor在backgroundColor的上一层)
appear.backgroundColor = UIColor(red: 52/255, green: 146/255, blue: 233/255, alpha: 1.0);
//去掉navigationBar下的黑线
appear.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
appear.shadowImage = UIImage();
//设置左右两侧的颜色
appear.tintColor = UIColor.whiteColor();
//设置标题样式
appear.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Heiti SC", size: 18.0)!];1) appear.translucent配置在早期系统会崩溃,不建议配置,建议在ViewController中配置
2) 在translucent=true也就是透明时,barTintColor不起作用,backgroundColor起作用
3) 在translucent=false也就是不透明时,barTintColor起作用,backgroundColor不起作用
4) 在translucent=false也就是不透明时,可以去掉navigationBar下的黑线,代码如下
//去掉navigationBar下的黑线
appear.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
appear.shadowImage = UIImage();5) 在translucent=true也就是透明时,如果用扇面的方式去掉navigationBar下的黑线,会导致状态栏颜色与navigationBar的背景色不同,暂没找到好的解决方法
6) 设置navigationBar背景透明的方式 跟去黑线的方式相同