Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController resets the status bar style to .Default when the search bar becomes the first responder

Tags:

ios

swift

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?

like image 512
Gruntcakes Avatar asked Oct 15 '25 14:10

Gruntcakes


1 Answers

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.

like image 177
Gruntcakes Avatar answered Oct 18 '25 03:10

Gruntcakes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!