3
votes

I'm using a UISearchController and adding its search bar to a table view controller's header:

enter image description here

The issues I'm facing is that when the user starts to type into the search bar, making it the first responder, then the status bar style is reverting to the .Default:

enter image description here

I'm using preferredStatusBarStyle() to set the style in the view controller. If instead I use the info.plist to set the style and additionally set the 'View Controller Based Status Appearance' key to NO, then the style stays as Light.

However it is not an option to set the 'View Controller Based Status Appearance' key to NO as I have some VCs that require the default style and some that require the light style.

I tried resetting the style to light on a search bar delegate method (which gets invoked when it becomes first responder) i.e.:

func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool
{
    UIApplication.sharedApplication().statusBarStyle = .LightContent
    self.setNeedsStatusBarAppearanceUpdate()
    return true
}

But that didn't work either.

Any suggestions?

2

2 Answers

3
votes

I don't know if there's another way, but I solved it by implementing a UISearchController derived class and using that instead:

class MySearchController : UISearchController
{
   // various init overrides ...

    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }
}

Or if a navigation controller is being used then this will prevent preferredStatusBarStyle() from being called, in which case use this instead:

UIApplication.sharedApplication().statusBarStyle = .LightContent

AND

Make sure in the info.plist "View controller-based status bar appearance" is set to NO.

0
votes

Update for SWIFT 3.0

When a navigation controller is being called:

UIApplication.shared.statusBarStyle = .lightContent (on viewDidLoad)

+

set View controller-based status bar appearance = NO on info.plist