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

标签 swift uisearchcontroller

我遵循了 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/

相关文章:

uisearchbar - iOS 11 UISearchBar 不接受 tintColor

swift - 有条件地激活 iOS8+ 的 UISearchController

ios - 如何在 Swift 中实现 didReceiveMemoryWarning?

swift - 如何使用mlmodel对UIImage进行预测?

objective-c - UISearchBar 消失在 UINavigationBar 后面

ios - 从 UISearchController 过滤的搜索结果中删除

uitableview - UIViewController 中的 UISearchController

ios - 界面生成器中的图像和标签与 TableView 单元格中的数据重叠

ios - 使用 AudioKit 的量化序列

ios - 重用单元格效果不佳 - TableView