UISearchController changing status bar color on invocation

后端 未结 6 1553
执念已碎
执念已碎 2021-02-08 18:09

I have the following code in my app, specifically in viewDidLoad: that sets up my UISearchController.

self.searchController = [[UISear         


        
相关标签:
6条回答
  • 2021-02-08 18:14

    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.

    0 讨论(0)
  • 2021-02-08 18:18

    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)
    0 讨论(0)
  • 2021-02-08 18:19

    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)

    0 讨论(0)
  • 2021-02-08 18:26

    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.

    0 讨论(0)
  • 2021-02-08 18:28

    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
    
    0 讨论(0)
  • 2021-02-08 18:29

    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.

    0 讨论(0)
提交回复
热议问题