Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Status Bar Background Color in Swift 3

In XCode 7.3.x ill changed the background Color for my StatusBar with:

func setStatusBarBackgroundColor(color: UIColor) { guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {     return } statusBar.backgroundColor = color } 

But it seems that this is not working anymore with Swift 3.0.

Ill tried with:

func setStatusBarBackgroundColor(color: UIColor) { guard  let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {     return } statusBar.backgroundColor = color } 

But it gives me:

this class is not key value coding-compliant for the key statusBar. 

Any Ideas how to change it with XCode8/Swift 3.0?

like image 657
derdida Avatar asked Oct 01 '16 02:10

derdida


People also ask

How do I change the background color on my status bar?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar.

How do I change the color of my status bar in Swift?

Open your info. plist and set UIViewControllerBasedStatusBarAppearance to false . In the first function in AppDelegate. swift , which contains didFinishLaunchingWithOptions , set the color you want.

How do I change the color of my status bar in react native?

To change the Android status bar color with React Native, we can set the backgroundColor prop of the StatusBar . to set the backgroundColor prop of StatusBar to 'green' . Now the status bar would be green.


2 Answers

extension UIApplication {     var statusBarView: UIView? {         if responds(to: Selector(("statusBar"))) {             return value(forKey: "statusBar") as? UIView         }         return nil     } }  UIApplication.shared.statusBarView?.backgroundColor = .red 

Update for iOS 13

App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.

Refer to How to change the status bar background color and text color on iOS 13?

like image 137
Callam Avatar answered Oct 17 '22 07:10

Callam


"Change" status bar background color:

let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame) let statusBarColor = UIColor(red: 32/255, green: 149/255, blue: 215/255, alpha: 1.0) statusBarView.backgroundColor = statusBarColor view.addSubview(statusBarView) 

Change status bar text color:

override var preferredStatusBarStyle: UIStatusBarStyle {     return .lightContent } 

Update: please note that the status bar frame will change when the view is rotated. You could update the created subview frame by:

  • Using the autoresizing mask: statusBarView.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
  • Observing NSNotification.Name.UIApplicationWillChangeStatusBarOrientation
  • Or overriding viewWillLayoutSubviews()
like image 38
Daniel Illescas Avatar answered Oct 17 '22 06:10

Daniel Illescas