【问题标题】:Search cursor disappearing when cancel button hidden隐藏取消按钮时搜索光标消失
【发布时间】:2017-03-18 03:36:38
【问题描述】:

我按照this post 中的重要指示对不显示取消按钮的搜索栏和搜索控制器进行了子类化。但是,当我开始编辑时,我的搜索栏中现在没有光标。我已经尝试为搜索栏设置色调,我在许多帖子中都将其视为各种委托方法的答案。从技术上讲,色调设置正确,正如我通过将搜索控制器设置为标准 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
}

有没有人遇到过这种情况?谢谢:)

【问题讨论】:

    标签: swift uisearchcontroller


    【解决方案1】:

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

    在您的 SearchBarWithoutCancel 中删除 layoutSubviews 并覆盖 setShowsCancelButton :

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

    objective-c 版本

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

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 2013-11-18
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多