gpt4 book ai didi

ios - 当我点击 searchBar 时,如何禁用 UISearchController.search Bar 的取消按钮?

转载 作者:行者123 更新时间:2023-11-28 05:54:50 28 4
gpt4 key购买 nike

我想在点击搜索栏时禁用右取消按钮。由于使用 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
}

最佳答案

您可以创建自定义类和子类 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链接以获取更多详细信息。

关于ios - 当我点击 searchBar 时,如何禁用 UISearchController.search Bar 的取消按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51729407/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com