【问题标题】:How to disable cancel button of UISearchController.seachBar when i tap at searchBar?当我点击 searchBar 时如何禁用 UISearchController.search Bar 的取消按钮?
【发布时间】:2018-08-07 14:36:04
【问题描述】:

我想在点击搜索栏时禁用右取消按钮。 由于使用 Google Place 搜索,我应该使用 searchController?.searchBar 我尝试在

处禁用取消按钮
func presentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.showsCancelButton = false
}

但是当我点击搜索栏时,我看到取消按钮是如何出现和消失的,这看起来很难看 请给我建议!

    override func viewDidLoad() {
    super.viewDidLoad()

    resultsViewController = GMSAutocompleteResultsViewController()
    resultsViewController?.delegate = self

    searchController = UISearchController(searchResultsController: resultsViewController)
    searchController?.searchResultsUpdater = resultsViewController

    searchController?.searchBar.sizeToFit()
    navigationItem.titleView = searchController?.searchBar
    searchController?.searchBar.placeholder = searchBarPlaceholderText
    searchController?.searchBar.tintColor = #colorLiteral(red: 0.1019607843, green: 0.5490196078, blue: 1, alpha: 1)
    searchController?.searchBar.delegate = self
    searchController?.delegate = self
    searchController?.searchBar.searchBarStyle = .prominent

    definesPresentationContext = true

    mapView.delegate = self
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest

    checklocationAuthorizationStatus()

    locationManager.requestWhenInUseAuthorization()
    locationManager.requestLocation()
    locationManager.startUpdatingLocation()
}


func presentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.showsCancelButton = false
}

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    您可以创建自定义类和子类 UISearchBar 和 UISearchViewController。 例如:-

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

    现在创建 CustomizedSearchBar 的对象并在其他 viewController 中使用它。

    或者你可以创建一个自定义的searchViewController,如下:

    class CustomizedSearchController: UISearchController, UISearchBarDelegate {
    
        lazy var _searchBar: CustomSearchBar = {
            [unowned self] in
            let result = CustomSearchBar(frame: CGRectZero)
            result.delegate = self
    
            return result
        }()
    
        override var searchBar: UISearchBar {
            get {
                return _searchBar
            }
        }
    }
    

    请关注this链接了解更多详情。

    【讨论】:

      猜你喜欢
      • 2017-12-14
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      相关资源
      最近更新 更多