【发布时间】:2017-10-25 03:34:20
【问题描述】:
我阅读了有关 stackoverflow 的所有问题,但没有一个可以解决我的问题。我创建了一个 UICollectionView 并在导航栏中锚定了一个搜索栏,而不使用情节提要!
class UserSearchController: UICollectionViewController ,.. {..
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Enter username"
sb.barTintColor = UIColor.gray
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
sb.delegate = self
sb.showsCancelButton = true
return sb
}() ....
// adding the searchBar to the collectionView as subView and
anchoring it to the navigationBar
搜索栏显示在导航栏内,一切正常。当我尝试将范围栏添加到搜索栏时会出现问题。我将以下属性添加到我的搜索栏
sb.showsScopeBar = true
sb.scopeButtonTitles = ["Username", "Hashtag"]
scopeBar 隐藏在 navigationBar 后面,并且 navBar 不会自动增加其高度。你只看到一个灰色的背景。
将我的代码添加到一个简单的 UiViewController 一切正常
此代码在普通 VIewController 中工作,其中 searchBar 作为 subView 添加并锚定到视图。
lazy var searchBar: UISearchBar = {
let sb = UISearchBar()
sb.placeholder = "Enter username"
sb.barTintColor = UIColor.gray
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.rgb(red: 230, green: 230, blue: 230)
sb.delegate = self
sb.scopeButtonTitles = ["Username", "Hashtag"]
sb.tintColor = UIColor.black
sb.barTintColor = UIColor.lightGray
return sb
}()
public func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
searchBar.showsScopeBar = true
searchBar.sizeToFit()
searchBar.setShowsCancelButton(true, animated: true)
return true
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchBar.showsScopeBar = false
searchBar.endEditing(true)
}
我的 searchBar 如下所示。如果您开始键入范围栏,则会出现,如果您单击取消,它就会消失。我想要相同的,但只在 navBar 内部:
如何在没有情节提要的情况下在导航栏中添加带有 scopeBar 的 searchBar。有没有简单的方法。请您参考我的代码并使用我的代码向我展示它是如何工作的。我还尝试增加导航栏的大小,但它应该会自动工作,我不知道这是否是正确的方法。
【问题讨论】:
-
您可以尝试放弃
scopeBar,并在搜索栏下方添加UISegmentController,以根据所选细分过滤您的搜索 -
我为我的 collectionView 创建了一个标题,并在我的标题中添加了分段控件。问题是我无法根据所选段更改我的 collectionView 的内容。我已经问过这个问题,他们建议使用 searchBar 中的 scopeBar 属性,现在您建议使用 segmentedControl :)
-
哦,我很抱歉,我只是想提供一个不同的选择。您能否详细说明为什么您不能根据所选细分更改搜索内容?我看看能不能帮忙
-
感谢您的帮助。该问题在上面的链接中进行了描述。你能回答这个问题吗?
标签: ios swift uinavigationcontroller uisearchbar uisearchbardelegate