3

I want to know if there is a way to change the iOS 7 status bar text color besides black and white color?

bufferoverflow76
  • 705
  • 1
  • 7
  • 21
  • http://stackoverflow.com/questions/21465486/change-status-bar-text-color-ios-other-than-black-or-white – iBhavin Jul 31 '14 at 08:11
  • 1
    possible duplicate of [How to change Status Bar text color in iOS 7.](http://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios-7) – Rajesh Jul 31 '14 at 08:12
  • @iDev: No, I have read the link before. But, like most of the question, it only set the text color to be black or white. What I want is to make the text color to be green. Is it possible? – bufferoverflow76 Aug 07 '14 at 06:05

2 Answers2

6

Put this in your AppDelegate application:didFinishLaunchingWithOptions:

Swift 4.2:

if UIApplication.shared.responds(to: Selector(("statusBar"))),
    let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView,
    statusBar.responds(to: #selector(getter: CATextLayer.foregroundColor)) {
    statusBar.setValue(UIColor.red, forKey: "foregroundColor")
}

Swift 3.0:

Just in case Apple decides to change the naming of the class (highly unlikely) we shall add some type-safety.

if application.responds(to: Selector(("statusBar"))),
   let statusBar = application.value(forKey: "statusBar") as? UIView,
   statusBar.responds(to: Selector(("foregroundColor"))) {
   statusBar.setValue(UIColor.red, forKey: "foregroundColor")
}

Swift 2.0:

application.valueForKey("_statusBar")?.setValue(UIColor.redColor(), forKey: "_foregroundColor")

Objective-C:

[[application valueForKey:@"_statusBar"] setValue: [UIColor redColor] forKey: @"_foregroundColor"];

It's hard to say if your app will be rejected from the app store. Using KVC to access the _statusBar property will not get your app rejected as the UIApplication class itself is not hidden.

That being said, neither is the UIStatusBar class hidden, ie. it's not in the SDK's PrivateFrameworks directory nor marked with __attribute__((visibility("hidden"))) nor does the class name begin with an underscore.

If your app was rejected because of using this please comment below so I can update the answer.

thatmarcel
  • 370
  • 3
  • 15
Mark Bourke
  • 9,052
  • 7
  • 21
  • 29
  • This will likely break if services such as a call or location or microphone are used. It's good to add the same code in a notification handler for `UIApplicationDidChangeStatusBarFrame`. – Leo Natan Dec 28 '16 at 16:07
  • @LeoNatan Would it not be better to use KVO on the `_foregroundColor` property and change the colour back to red there? Ofc guarding against the colour being the same to avoid recursion. – Mark Bourke Dec 28 '16 at 16:25
  • That key may not be KVO compliant. Indeed, most of UIKit isn't. – Leo Natan Dec 28 '16 at 16:26
  • Are you sure? It was my understanding that all Foundation Objective-C classes are compliant to KVO automatically. I know you need the `dynamic` keyword for swift. Is there something similar for Obj-C? – Mark Bourke Dec 28 '16 at 16:34
  • UIKit is not Foundation. ObjC always uses dynamic dispatch, while in Swift you need to explicitly state it. If your class inherits form `NSObject`, its public and internal methods and properties also use dynamic dispatch due to `@objc` compliance. – Leo Natan Dec 28 '16 at 16:37
  • UIColor inherits from NSObject though. Why would there be a problem observing it? – Mark Bourke Dec 28 '16 at 16:38
  • UIColor objects are immutable, what do you want to observe on colors? You want to observe a property that has changing colors, a property of `UIStatusBar`. A crash course on KVO compliance: if you change the value of the underlying instance variable by any other means than using the setter method, a property is no KVO compliant. Since `UIStatusBar` is a private class, there is no way of knowing it is KVO compliant in all cases. https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/UIKit.framework/UIStatusBar.h – Leo Natan Dec 28 '16 at 16:41
  • Suggest reading on KVO compliance: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOCompliance.html – Leo Natan Dec 28 '16 at 16:43
  • You're a genius! – Rudolf J Mar 06 '17 at 22:26
1

Theoretically this is possible. You will need to read about private api's on iOS. Here is o good place to start with UIStatusBar example:

http://b2cloud.com.au/tutorial/using-private-ios-apis/

Keep in mind that probably you won't be able to submit your app on Appstore if you would use private api.

ArturOlszak
  • 2,574
  • 2
  • 19
  • 20