状态栏

  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级别的两种修改方式

  • info.plist(当然这里可以直接在xcode->target->deployinfo->statue bar style中直接设置)
    <key>UIStatusBarStyle</key>
    <string>UIStatusBarStyleLightContent</string>
  • UIAplication
//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
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容