Hidden StatusBar reappears when SearchBar gets activated

后端 未结 1 794
孤独总比滥情好
孤独总比滥情好 2021-01-28 04:17

I have a simple app with a TableView but without a NavigationBar/NavigationController. The app also has a UISearchController with a SearchBar that is always visible. I tried to

相关标签:
1条回答
  • 2021-01-28 04:37

    Swift 3

    To selectively display the status bar, you must implement the following:

    Go to the Info.plist, add 'View controller-based status bar appearance' -> YES. This will give you access to being able to set the appearance based on the state of the prefersHiddendStatusBar variable for your specific view.

    The settings in Interface Builder are simply for simulated metrics; that is, what displays in StoryBoard objects while using Interface Builder.

    Next, you will need to create a way to store your conditional preference: "Do I want to display the status bar now?" In your view controller, create a boolean variable to hold this preference:

    var displayStatusBar: Bool = false
    

    Then, when you use the SearchController, you must tie into the specific delegate methods that fire when you interact with the searchbar. I recommend using:

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar)
    

    and

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar)
    

    In each of those delegate methods, you can set the displayStatusBar variable to true or false and then use the setNeedsStatusBarAppearanceUpdate() in each method. This will force a reload in the status bar. If you think it looks choppy, throw that code inside a UIView.animate(withDuration:_) completion block for a nice and smooth visual change.

    Finally, you need to override the View's preferred status variable and set it to the preference variable.

    override var prefersStatusBarHidden: Bool {
        return hideStatusBar
    }
    
    0 讨论(0)
提交回复
热议问题