I have the following code in my app, specifically in viewDidLoad:
that sets up my UISearchController
.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.definesPresentationContext = NO;
self.searchController.searchBar.scopeButtonTitles = @[];
self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
[_tableView setTableHeaderView:_searchController.searchBar];
Whenever the search bar (which is added to the tableView
) is invoked, the UIStatusBar
color changes from UIStatusBarStyleLightContent
to dark (white to black). Now, I figured out if I set,
self.definesPresentationContext = NO;
to the following:
self.definesPresentationContext = YES;
the issue is solved and the UIStatusBar
color is preserved. However, another issue arises. When self.definesPresentationContext
is set to YES
, upon invocation the search bar shifts down for some reason, coincidently (or rightfully so) right under where the bottom of the UIRefreshControl
displays on the tableView
.
Setting
View-controller based status bar appearance
toNo
is not a solution if you want the view controllers to define how the status bar looks.My solution consisted of two things:
definesPresentationContext
set toYES
extendedLayoutIncludesOpaqueBars
toYES
)As of iOS 10 (maybe earlier?), if you have "View controller-based status bar appearance" set to YES in your Info.plist, simply set the preferredStatusBarStyle in the UIViewController that the UISearchController is included in.
(you don't need to subclass or create a category/extension of UISearchController to override preferredStatusBarStyle... it uses the preferredStatusBarStyle that you set in your UIViewController)
Or we can write an extension on Swift (version 2, but you can translate it to 3 easily):
Where
Theme
is a class that regulate app colour theme.If you ViewController is inside a TabBarController then -
Instead of
self.definesPresentationContext = YES;
Use
self.tabBarController.definesPresentationContext = YES;
This worked for me in above scenario.
I needed full control over my status bar colour. I use the extensions found here to ensure that the visible view controller is setting the preferred status bar colour.
For me it was therefore necessary to override UISearchController and override
preferredStatusBarStyle
and return the style I wanted.The status bar that is displayed when the search controller is presented (is active) belongs to the search controller. To set the preferred status bar style you must add a category to UISearchController and then override the preferredStatusBarStyle method.
Below is an example of the implementation file of the category: