我有一个自定义的UISearchBar,当用户点击searchBar时,我想显示一个额外的按钮。所以,我使用scopeButtonTitles属性来显示其他按钮。但是,它没有出现在UISearchBar上。
customSearchController = CustomSearchController(searchResultsController: self, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: tableView.frame.size.width, height: 50.0), searchBarFont: UIFont.systemFont(ofSize: 16.0), searchBarTextColor: UIColor.white, searchBarTintColor: UIColor.primaryBlue())
customSearchController.hidesNavigationBarDuringPresentation = false
customSearchController.dimsBackgroundDuringPresentation = false
customSearchController.customSearchBar.placeholder = NSLocalizedString("Search", comment: "Search")
customSearchController.customSearchBar.accessibilityLabel = customSearchController.searchBar.placeholder
//Scope Buttons
customSearchController.customSearchBar.scopeButtonTitles = [NSLocalizedString("List", comment: "List"), NSLocalizedString("Map", comment: "Map")]
如果我没有使用自定义UISearchBar,它可以正常工作。实际问题是我要去定制UISearchBar。
答案 0 :(得分:1)
将UISearchBar
的{{3}}属性设置为true
,以显示scopeBar。
customSearchController.customSearchBar.showsScopeBar = true
修改:为此,您可以使用委托方法searchBarTextDidBeginEditing
并显示范围栏。
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
customSearchController.customSearchBar.showsScopeBar = true
//set the frame so it will show scope bar properly.
}
现在,如果您想在搜索后隐藏scoprBar,则可以使用searchBarCancelButtonClicked
隐藏范围栏。
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
customSearchController.customSearchBar.showsScopeBar = false
//set the frame again
}