UISearchController changing status bar color on invocation

旧时模样 提交于 2019-12-03 06:24:44

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)

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)

Daniel Wood

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.

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!