UISearchController changing status bar color on in

2020-06-03 06:23发布

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.

6条回答
在下西门庆
2楼-- · 2020-06-03 06:30

Setting View-controller based status bar appearance to No is not a solution if you want the view controllers to define how the status bar looks.

My solution consisted of two things:

  1. Make sure the presenting view controller has definesPresentationContext set to YES
  2. Make sure both the view controller that is pushed and the pushing view controller are laid out beneath the navigation bar (set extendedLayoutIncludesOpaqueBars to YES)
查看更多
小情绪 Triste *
3楼-- · 2020-06-03 06:45

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.

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

(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)

查看更多
做自己的国王
4楼-- · 2020-06-03 06:45

Or we can write an extension on Swift (version 2, but you can translate it to 3 easily):

extension UISearchController {

   override public func preferredStatusBarStyle() -> UIStatusBarStyle{
      if Theme.lightTheme() {
          return UIStatusBarStyle.Default
      }
      else {
          return UIStatusBarStyle.LightContent
      }
   }
}

Where Theme is a class that regulate app colour theme.

查看更多
再贱就再见
5楼-- · 2020-06-03 06:48

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.

查看更多
再贱就再见
6楼-- · 2020-06-03 06:50

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 star\"
7楼-- · 2020-06-03 06:51

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:

@implementation UISearchController (Customization)

-(UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
查看更多
登录 后发表回答