gpt4 book ai didi

ios - 如何为自定义的 UISearchBar 实现 scopeButtonTitles?

转载 作者:行者123 更新时间:2023-11-28 06:26:09 25 4
gpt4 key购买 nike

我有一个自定义的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com