取消按钮隐藏时,搜索光标消失

时间:2017-03-18 03:36:39

标签: swift uisearchcontroller

我按照this post中的重要指示来继承搜索栏并搜索未显示取消按钮的控制器。但是,当我开始编辑时,我的搜索栏中现在没有光标。我已经尝试在各种委托方法中设置搜索栏的色调,我在很多帖子中看到了这个答案。技术上正确设置了色调,正如我通过将搜索控制器设置为标准UISearchController来测试时所看到的那样。但是一旦我将它设置为我的子类SearchControllerWithoutCancel,光标就会消失。

以下是我的子类:

class SearchBarWithoutCancel: UISearchBar {
override func layoutSubviews() {
    super.layoutSubviews()
    setShowsCancelButton(false, animated: false)
}

}

class 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
    }
}

}

这是我从addSearchController

调用的viewDidLoad()方法
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
}

之前有没有遇到过这个?谢谢:))

1 个答案:

答案 0 :(得分:0)

实际上,当取消按钮被隐藏时,光标色调颜色会自行重置

在SearchBarWithoutCancel中

删除layoutSubviews并覆盖setShowsCancelButton:

override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
    //nothing
}}

objective-c version

-(void) setShowsCancelButton:(BOOL)show animated:(BOOL)animated
{
    //nothing
}