我遵循了 this post 中的重要指示将不显示取消按钮的搜索栏和搜索 Controller 子类化。但是,当我开始编辑时,我的搜索栏中现在没有光标。我已经尝试在各种委托(delegate)方法中设置搜索栏的色调,我在许多帖子中都将其视为答案。色调在技术上设置正确,正如我通过将搜索 Controller 设置为标准 UISearchController
来测试它时所看到的那样。但是一旦我将它设置为我的子类 SearchControllerWithoutCancel
,光标就会消失。
这是我的子类:
class SearchBarWithoutCancel: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
setShowsCancelButton(false, animated: false)
}
类 SearchControllerWithoutCancel: UISearchController, UISearchBarDelegate {
lazy var _searchBar: SearchBarWithoutCancel = {
[unowned self] in
let result = SearchBarWithoutCancel(frame: .zero)
result.delegate = self
return result
}()
override var searchBar: UISearchBar {
get {
return _searchBar
}
}
这是我从 viewDidLoad()
调用的 addSearchController
方法
func addSearchController() {
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.searchBarStyle = .minimal
searchController.searchBar.tintColor = UIColor.black
self.definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}
有没有人遇到过这个?谢谢:)
最佳答案
确实,当取消按钮被隐藏时,光标色调会自行重置
在您的 SearchBarWithoutCancel 中删除 layoutSubviews 并覆盖 setShowsCancelButton :
override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
//nothing
}}
目标-c 版本
-(void) setShowsCancelButton:(BOOL)show animated:(BOOL)animated
{
//nothing
}
关于swift - 隐藏取消按钮时搜索光标消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870164/