Z Jones
Z Jones

Reputation: 2035

UISearchController won't honor hidden status bar when search bar has focus

I've got my app hiding the status bar via the plist entry + this in the appdelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  [application setStatusBarHidden:YES];  

When my view loads, the status bar is hidden. As soon as I tap and give focus to the search bar, the status bar shows up. When the search bar loses focus, the status bar goes away again.

How can I prevent the status bar from appearing when the search bar has focus?

Upvotes: 2

Views: 1235

Answers (2)

Don Miguel
Don Miguel

Reputation: 912

When the search bar is tapped on the parent controller the control of how to present (hide/show) the status bar is given over to the search controller.

Setting "View controller-based status bar appearance" to NO in the info.plist worked to an extent, preventing the status bar to appear when tapping search, although this kept the status bar from appearing on other view controllers too; an unwanted behavior.

Instead I think it's more accurate to sub-class UISearchController and override the prefersStatusBarHidden method there. (I have also kept "View controller-based status bar appearance" set to YES in the info.plist).

So:

@implementation MySearchController

// Override so it does not show the status bar when active
- (BOOL)prefersStatusBarHidden {
     return YES;
}    
...
@end

And within the presenting controller:

_searchController = [[MySearchController alloc] initWithSearchResultsController:nil];

Upvotes: 3

abanet
abanet

Reputation: 1387

It happened to me!! I had "Status bar is initially hidden" in info.plist and also the func (swift in my case):

override func prefersStatusBarHidden() -> Bool {
    return true;
}

But as soon as I tap the search bar the status bar shows up as ZJones just says.

I fix this adding in info.plist this entry:

"View controller-based status bar appearance" as NO

easy but really difficult to find!!

From Apple documentation:

iOS 7 gives view controllers the ability to adjust the style of the status bar while the app is running. A good way to change the status bar style dynamically is to implement preferredStatusBarStyle and—within an animation block—update the status bar appearance and call setNeedsStatusBarAppearanceUpdate.

Note

If you prefer to opt out of having view controllers adjust the status bar style—and instead set the status bar style by using the UIApplicationstatusBarStyle method—add the UIViewControllerBasedStatusBarAppearance key to an app’s Info.plist file and give it the value NO.

I hope it works for you!

Upvotes: 3

Related Questions