Hidden StatusBar reappears when SearchBar gets activated

蓝咒 提交于 2019-12-11 16:49:51

问题


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 hide the status bar with prefersStatusBarHidden and that works fine. Until the SearchBar is activated. Then the status bar will appear again.

How can I prevent this and keep the status bar hidden?

var cityRepository:CityRepository?
var searchController:UISearchController?

override func viewDidLoad()
{
    self.cityRepository = CityRepository()
    self.searchController = UISearchController(searchResultsController: nil)
    self.searchController!.searchResultsUpdater = self
    self.searchController!.dimsBackgroundDuringPresentation = false
    self.definesPresentationContext = true
    self.tableView.tableHeaderView = searchController?.searchBar

    //* Already tried this ....
    self.edgesForExtendedLayout = UIRectEdge.None
    self.extendedLayoutIncludesOpaqueBars = true
    self.automaticallyAdjustsScrollViewInsets = false

    super.viewDidLoad()
}

override func prefersStatusBarHidden() -> Bool
{
    return true
}

回答1:


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
}


来源:https://stackoverflow.com/questions/35563770/hidden-statusbar-reappears-when-searchbar-gets-activated

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