状态栏

2017-03-25  本文已影响41人  Bearger
  1. 配置项(info.plist)
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <true/>

Apps default to using the new view controller-based status bar >management system. To opt out of this, add a value of NO for the UIViewControllerBasedStatusBarAppearance key to your Info.plist.

UIViewControllerBasedStatusBarAppearance默认是true
UIViewControllerBasedStatusBarAppearance == true的时候,ViewControllerNavigationController才能对StatueBar的前景样式进行更改。
UIViewControllerBasedStatusBarAppearance == false的时候,需要Application级别对其进行修改才能生效。

Applicaiton级别的两种修改方式

    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
//9以后被遗弃了
application.setStatusBarStyle(.default, animated: false)
  1. ViewController中对StatueBar进行修改
    在ViewController中重写相应的方法并调用需要更新
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    override var prefersStatusBarHidden: Bool{
        return true
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //通知系统咱需要更新状态栏
        self.setNeedsStatusBarAppearanceUpdate()
    }

完成了上一步后,重写的两个方法没有导航的情况下是可以的。可是一但加了导航NavigationControllerself.setNeedsStatusBarAppearanceUpdate()之后,系统会调用导航控制器中的preferredStatusBarStyleprefersStatusBarHidden。所以,还需要继承导航控制器并重写相应的方法:

class BearNavigationViewController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //就是这两个方法
    override var childViewControllerForStatusBarStyle: UIViewController?{
        return self.topViewController
    }
    override var childViewControllerForStatusBarHidden: UIViewController?{
        return self.topViewController
    }
}
上一篇下一篇

猜你喜欢

热点阅读