0

i searched and searched and so far i only found answers for:

  1. Choosing a specific color for specific view controllers
  2. Making Status Bar color change according to its current View Controller

i need to know: is it possible to decide on a universal color for status bar that will appear everywhere in the application?

Thank you for the help

MarkosDarkin
  • 307
  • 3
  • 17

4 Answers4

0
  1. Set "View controller-based status bar appearance"
  2. (UIViewControllerBasedStatusBarAppearance) to YES in your Info.plist. (YES is the default, so you can also just leave this value out of your plist.)
  3. In your viewDidLoad method, call [self setNeedsStatusBarAppearanceUpdate].
  4. Implement preferredStatusBarStyle, returning the status bar style that you want for this view controller:

    - (UIStatusBarStyle) preferredStatusBarStyle { 
        return UIStatusBarStyleLightContent; 
    }
    
Jamal
  • 747
  • 7
  • 22
  • 31
Harshal.y
  • 26
  • 4
  • 1
    im really not sure, but isnt this the method to make each ViewController show its own Status Bar color? i wrote (number 1) that i already found that. i want to be able to only define it ONCE. thanks anyway though – MarkosDarkin Aug 16 '16 at 11:26
0

On way to achieve this is to create a UIView with the proper position and dimension, then add it to the UIWindow directly :

class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var statusBarColorView: UIView?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
window?.makeKeyAndVisible()

    let v = UIView(frame: CGRect(x: 0, y: 0, width: window?.frame.size.width ?? 1024, height: 20))
    v.backgroundColor = UIColor.white
    v.translatesAutoresizingMaskIntoConstraints = false
    window?.addSubview(v)
    window?.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat: "V:|-0-[v(==20)]",
        options: [],
        metrics: nil,
        views: ["v": v]))
    window?.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|-0-[v]-0-|",
        options: [],
        metrics: nil,
        views: ["v": v]))

    return true
}

The problem with that approach is that you'll need to hide / show it according to the device orientation / currently displayed view controller yourself.

Changing color will look like this :

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
        appDelegate.statusBarColorView?.backgroundColor = UIColor.blue
    }
}

You'll surely want to customize UIStatusBarStyle according to your color, you can achieve this by selecting YES for the key UIViewControllerBasedStatusBarAppearance in your Info.plist

dulgan
  • 6,563
  • 3
  • 38
  • 45
-1

You can add in app delegate as well:

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
…
[application setStatusBarStyle:UIStatusBarStyleLightContent];
…
}

Or another way as I discussed in the first comment:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file.
  2. In the viewDidLoad do a [self setNeedsStatusBarAppearanceUpdate];
  3. Add the following method:

    - (UIStatusBarStyle)preferredStatusBarStyle
    { 
        return UIStatusBarStyleLightContent; 
    }
    

Note: this will not work if you have a navigation bar.

Jamal
  • 747
  • 7
  • 22
  • 31
Harshal.y
  • 26
  • 4
-1

Edit So, thanks to HAS, i understood this is not a good answer. it works, but its using a private API (accessing statusBarWindow and statusBar with KVC) so its probably wont fly with Apple. this is NOT the answer :)

Well, after a long research i found the way to do it and its quite simple. Again, im trying to give the status bar a custom color like Black, White or Orange. The way to accomplish this is by:

  1. go to Info.plist and add (if not already there) "View controller-based status bar appearance" and set it to: NO
  2. Go to project's Deployment Info and set 'Status Bar Style' to 'Light' or 'Default' (depends on the color you chose. for Black, you should choose 'Light')
  3. go to AppDelegate.swift and add the following function:

    func setStatusBarBackgroundColor(color: UIColor){
        guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
            return
        }
    
        statusBar.backgroundColor = color
    }
    
  4. in AppDelegate.swift, go to this function:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    

    and inside it call the function we made on 3, meaning: setStatusBarBackgroundColor(TheColorYouWant)

thats it :) it works on all views and no further code needs to be written. please feel free to correct me if i made any mistakes or if theres a way to improve the code in any way.

Thank you so much for those who took the time to help

MarkosDarkin
  • 307
  • 3
  • 17
  • 1
    This uses private API and probably won't let you submit your app to the AppStore. – HAS Aug 16 '16 at 15:47
  • 1
    `statusBarWindow` and `statusBar` are both private. Otherwise they would be listed here https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/ and you would not need to use KVC to get these properties. – HAS Aug 17 '16 at 13:20
  • Well, cant say im not convinced. appreciate it that you told me that. so is it not possible to achieve such a thing in a proper, not private API, way? im kinda desperate :P – MarkosDarkin Aug 17 '16 at 14:46
  • 1
    I'm not aware of any way :( – HAS Aug 17 '16 at 15:27