UISearchController searchBar showsCancelButton not being respected

后端 未结 12 2313
北荒
北荒 2021-02-13 03:58

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 t

相关标签:
12条回答
  • 2021-02-13 04:05

    I had to correct by putting in a little hack...

    Setting the alpha to 0.0 on viewDidLoad because he screen will flash.

    Before you ask...willPresentSearchController will not work.

    extension GDSearchTableViewController: UISearchControllerDelegate {
        func didPresentSearchController(searchController: UISearchController) {
            searchController.searchBar.setShowsCancelButton(false, animated: false)
            searchController.searchBar.becomeFirstResponder()
            UIView.animateWithDuration(0.1) { () -> Void in
                self.view.alpha = 1.0
                searchController.searchBar.alpha = 1.0
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-13 04:07

    This worked for me (iOS 10):

    - (void)viewWillAppear:(BOOL)animated {
          [super viewWillAppear:animated];
          self.searchController.searchBar.showsCancelButton = NO;
    }
    
    0 讨论(0)
  • 2021-02-13 04:07

    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.

    0 讨论(0)
  • 2021-02-13 04:08

    You can subclass UISearchBar and override method layoutSubviews

    super.layoutSubviews()
    self.showsCancelButton = false
    
    0 讨论(0)
  • 2021-02-13 04:12

    I agree, it seems like a bug. The problem is that the searchController keeps resetting the showsCancelButton property of the searchBar. I found a solution that involves:

    1. subclassing UISearchBar to ignore setShowsCancelButton.
    2. to make the searchController use that subclass, you have to subclass UISearchController.
    3. And then you find that the searchBar is not triggering the search controller's delegate methods, so you have to trigger them separately...

    Convoluted, but it seems to do the trick. You can find the full answer here.

    0 讨论(0)
  • 2021-02-13 04:14

    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];
    }
    
    0 讨论(0)
提交回复
热议问题