作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义的 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 时。
最佳答案
设置showsScopeBar
UISearchBar
的属性设置为 true
以显示 scopeBar。
customSearchController.customSearchBar.showsScopeBar = true
编辑:为此,您可以使用委托(delegate)方法 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
}
关于ios - 如何为自定义的 UISearchBar 实现 scopeButtonTitles?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41830610/
我是一名优秀的程序员,十分优秀!