UISearchController searchBar showsCancelButton not

2020-06-08 13:54发布

I've added a UISearchController to my application and set it's searchBar to the titleView of my navigationItem.

This works but I am seeing the cancel button despite having set showsCancelButton to false.

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


    // Configure the searchBar
    searchController.searchBar.placeholder = "Find Friends..."
    searchController.searchBar.sizeToFit()
    searchController.searchBar.showsCancelButton = false

    self.definesPresentationContext = true

    navigationItem.titleView = searchController.searchBar

enter image description here

标签: ios xcode swift
12条回答
干净又极端
3楼-- · 2020-06-08 14:10

Easy solution in Swift3 - we need to make CustomSearchBar without cancel button and then override the corresponding property in new CustomSearchController:

class CustomSearchBar: UISearchBar {

override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
    super.setShowsCancelButton(false, animated: false)
}}


class CustomSearchController: UISearchController {

lazy var _searchBar: CustomSearchBar = {
    [unowned self] in
    let customSearchBar = CustomSearchBar(frame: CGRect.zero)
    return customSearchBar
    }()

override var searchBar: UISearchBar {
    get {
        return _searchBar
    }
}}

In MyViewController I initialize and configure searchController using this new custom subclass:

    var mySearchController: UISearchController = ({
    // Display search results in a separate view controller
    //        let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
    //        let alternateController = storyBoard.instantiateViewController(withIdentifier: "aTV") as! AlternateTableViewController
    //        let controller = UISearchController(searchResultsController: alternateController)
    let controller = CustomSearchController(searchResultsController: nil)
    controller.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. iceland)", comment: "")
    controller.hidesNavigationBarDuringPresentation = false
    controller.dimsBackgroundDuringPresentation = false
    controller.searchBar.searchBarStyle = .minimal
    controller.searchBar.sizeToFit()
    return controller
})()
查看更多
\"骚年 ilove
4楼-- · 2020-06-08 14:10

This worked for me (iOS 10):

- (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      self.searchController.searchBar.showsCancelButton = NO;
}
查看更多
小情绪 Triste *
5楼-- · 2020-06-08 14:13

I try to help you man but I'm not sure that I find the real problem.

According to Apple Documentation:

showsCancelButton

boolean property that indicating whether the cancel button is displayed

But for hide the cancel button maybe you should use:

setShowsCancelButton(_:animated:)

enter image description here

I hope that can be helpful.

查看更多
冷血范
6楼-- · 2020-06-08 14:23

We wanted the search bar to have no Cancel button initially, but have it appear when the user tapped in the search bar. Then we wanted the Cancel button to disappear if user tapped Cancel, or otherwise the search bar lost first responder.

What finally worked for me:

On create:

searchBar.showsCancelButton = NO;

We use a subclass of UISearchBar and override searchBarShouldBeginEditing thusly:

-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
    self.showsCancelButton = YES;
    return YES;
}

We also override resignFirstReponder (in the UISearchBar subclass) thusly:

-(BOOL)resignFirstResponder
{
    self.showsCancelButton = NO;
    return [super resignFirstResponder];
}
查看更多
唯我独甜
7楼-- · 2020-06-08 14:27

You can subclass UISearchBar and override method layoutSubviews

super.layoutSubviews()
self.showsCancelButton = false
查看更多
登录 后发表回答