Search code examples
iosobjective-cuikituisearchbaruisearchcontroller

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?


Solution

  • 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];