当 SearchBar 被激活时隐藏的 StatusBar 重新出现 [英] Hidden StatusBar reappears when SearchBar gets activated

查看:23
本文介绍了当 SearchBar 被激活时隐藏的 StatusBar 重新出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 TableView 但没有 NavigationBar/NavigationController 的简单应用程序.该应用程序还有一个带有始终可见的 SearchBar 的 UISearchController.我试图用 prefersStatusBarHidden 隐藏状态栏,效果很好.直到 SearchBar 被激活.然后状态栏会再次出现.

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
}

推荐答案

Swift 3

要选择性地显示状态栏,您必须实现以下内容:

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

转到Info.plist,添加'基于控制器的状态栏外观' -> YES.这将使您能够根据特定视图的 prefersHiddendStatusBar 变量的状态设置外观.

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.

Interface Builder 中的设置仅用于模拟指标;也就是说,使用 Interface Builder 时 StoryBoard 对象中显示的内容.

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

然后,当您使用 SearchController 时,您必须绑定到与搜索栏交互时触发的特定委托方法.我建议使用:

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)

func searchBarCancelButtonClicked(_ searchBar: UISearchBar)

在每个委托方法中,您可以将 displayStatusBar 变量设置为 true 或 false,然后在每个方法中使用 setNeedsStatusBarAppearanceUpdate().这将强制在状态栏中重新加载.如果您认为它看起来不连贯,请将该代码放入 UIView.animate(withDuration:_) 完成块中,以获得漂亮且平滑的视觉变化.

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
}

这篇关于当 SearchBar 被激活时隐藏的 StatusBar 重新出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆