3

Since apple introduced UIViewControllerBasedStatusBarAppearance and preferredStatusBarStyle in iOS 7 I'm trying to understand whats best practice to change status bar color for built in view controller, like:

UIImagePickerController  
MFMailComposeViewController  
UISearchDisplayController  

for example, when using UISearchDisplayController, I want to change the status bar from light to dark when the search bar appears.
how can I do that? do I need to subclass UISearchDisplayController? maybe category?

and what about UIImagePickerController it has its own stack of view controllers, how can change the status bar style for all of them when presenting the photo picker?

until now I used the global why of

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];  

but now its all per-ViewController approach, so how would I modify controllers that are not mine?

Mario
  • 2,431
  • 6
  • 27
  • 34
  • UISearchDisplayController is not a view controller and it's deprecated. You can still implement `preferredStatusBarStyle` on your source view controller and return different statusbar style when you enter or leave searchDisplay. There is `setNeedsStatusBarAppearanceUpdate` to force UIKit to re-query statusbar style. – pronebird Dec 03 '14 at 14:12
  • @Andy you are right I didn't noticed its not a view controller (I just assume so because its managing its own table view..) and I must use it although its deprecated because I support iOS 7 as well – Mario Dec 05 '14 at 21:35

1 Answers1

1

Yes, you can subclass those classes to change the status bar appearance. According to UIImagePickerController, it's just a subclass of the UINavigationController, so, if you subclass and implement preferredStatusBarStyle in your subclass, all other view those appear on navigation will have the same status bar. Good Luck!

Fahri Azimov
  • 11,470
  • 2
  • 21
  • 29
  • Worth to add that you can do the same with `MFMailComposeViewController` regardless what Apple says about subclassing it, adding statusbar appearance method is not a crime. It's still a view controller regardless the portal to the outer process. – pronebird Dec 03 '14 at 14:06
  • Fair and @Andy any reason why not to use category instead of subclassing? – Mario Dec 05 '14 at 21:33
  • @Mario categories pollute environment. Normally, you don't use category for `viewDidLoad`, the same way you should not use category for `preferredStatusBarStyle`. Any monkey patching is generally a risky business. Although I don't see a problem with extending existing classes with categories, but replacing methods with categories is bad. Besides if you have two categories that replace the same method, the order in which they do this is pretty much undefined. – pronebird Dec 05 '14 at 21:52
  • @Andy when I use UIImagePickerController I want that the status bar will be white, but after a user choose a photo and goes to the crop controller I want no status bar, how can I do that? the crop controller is private – Mario Dec 07 '14 at 14:42
  • @Mario ask a separate question – pronebird Dec 07 '14 at 14:54
  • @Andy just did http://stackoverflow.com/questions/27344429/uiimagepickercontroller-status-bar-issue-in-crop – Mario Dec 07 '14 at 15:51