Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
880 views
in Technique[技术] by (71.8m points)

ios - Change Status Bar Color in Real Time

I am moving in a side menu view controller (ENSwiftSideMenu) with an animation. Since the side menu's background color is a pretty dark blue, I'd like to have the status bar, that is black by default, turn light while the menu is visible.
With View controller-based status bar appearance being set to YES (also tried NO) inside the info.plist, I tried to implement the following code which did, unfortunately, not work:

UIApplication.shared.statusBarStyle = .lightContent

Also, I tried this:

override func viewDidLoad() {
    super.viewDidLoad()
    setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .default
}

Any idea how to make it work?
Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try This Code:

In your AppDelegate:

     var navigationBarAppearace = UINavigationBar.appearance()
     navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()] // If you want to change title colour
     UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default 

//Update your plist with below code

     View controller-based status bar appearance = NO

In your ViewController:

    override func viewDidLoad() {
    super.viewDidLoad()

     // UIApplication.sharedApplication().statusBarStyle = .LightContent
    navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    navigationController?.navigationBar.translucent = true
}

func sideMenuWillOpen() {
    print("sideMenuWillOpen")
UIApplication.sharedApplication().statusBarStyle = .LightContent
}

func sideMenuWillClose() {
    print("sideMenuWillClose")
 UIApplication.sharedApplication().statusBarStyle = .Default 
}

enter image description here

Output from the above code.You can use some sort of UIAnimation to sync the effect..

Let me know.If the code works for you...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...