在导航栏中显示搜索栏而不在iOS 11上滚动 [英] Show search bar in navigation bar without scrolling on iOS 11

查看:314
本文介绍了在导航栏中显示搜索栏而不在iOS 11上滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将UISearchController附加到iOS 11上UITableViewController的 navigationItem.searchController 属性。这很好用:我可以使用漂亮的iOS 11风格搜索栏。

I’m attaching a UISearchController to the navigationItem.searchController property of a UITableViewController on iOS 11. This works fine: I can use the nice iOS 11-style search bar.

但是,我想在发布时看到搜索栏。默认情况下,用户必须在表格视图中向上滚动才能看到搜索栏。有谁知道这怎么可能?

However, I’d like to make the search bar visible on launch. By default, the user has to scroll up in the table view to see the search bar. Does anyone know how is this is possible?


左:启动后的默认情况。右:搜索栏可见(通过向上滚动)。我希望在发布后可以看到搜索栏,如右图所示。

我已经发现搜索栏可以通过将我的导航项的属性 hidesSearchBarWhenScrolling 设置为false。但是,这会导致搜索栏始终可见 - 即使向下滚动 - 这也不是我想要的。

I already found that the search bar can be made visible by setting the property hidesSearchBarWhenScrolling of my navigation item to false. However, this causes the search bar to always be visible — even when scrolling down —, which is not what I want.

推荐答案

以下内容使滚动条最初可见,然后允许它在滚动时隐藏:

The following makes the scroll bar visible at first, then allows it to hide when scrolling:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.hidesSearchBarWhenScrolling = false
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if #available(iOS 11.0, *) {
        navigationItem.hidesSearchBarWhenScrolling = true
    }
}

使用 isActive 没有做我想要的,它使滚动条激活(显示取消按钮等),当我想要的是它是可见的。

Using isActive didn't do what I wanted, it makes the scroll bar active (showing cancel button, etc.), when all I want is for it to be visible.

这篇关于在导航栏中显示搜索栏而不在iOS 11上滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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