Hide UISearchBar Cancel Button

后端 未结 11 835
夕颜
夕颜 2020-12-24 06:39

I have a UISearchDisplayController and UISearchBar hooked up to my ViewController via Outlets from my nib.

I\'d like to hide the cancel button so that the user never

相关标签:
11条回答
  • 2020-12-24 07:17

    If the cancel button shows up when editing the search field of the search bar you could do the following; subclass the search bar and have it implement the UITextFieldDelegateprotocol:

    @interface CustomAlignedSearchBar : UISearchBar<UITextFieldDelegate>
    

    Then implement textFieldDidBeginEditing: and do something like:

    - (void)textFieldDidBeginEditing:(UITextField *)textField{
        [self setShowsCancelButton:self.cancelButtonShown animated:NO];
    }
    

    This will make sure that the cancel button will not show up.

    0 讨论(0)
  • 2020-12-24 07:19
    class CustomSearchBar: UISearchBar {
    
        override func setShowsCancelButton(showsCancelButton: Bool, animated: Bool) {
            super.setShowsCancelButton(false, animated: false)
        }
    
    }
    
    class CustomSearchController: UISearchController, UISearchBarDelegate {
    
        lazy var _searchBar: CustomSearchBar = {
            [unowned self] in
            let customSearchBar = CustomSearchBar(frame: CGRectZero)
            customSearchBar.delegate = self
            return customSearchBar
        }()
    
        override var searchBar: UISearchBar {
            get {
                return _searchBar
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 07:22

    I had the same issue, but fixed it a different way.

    For those who can't or don't want to subclass UISearchDisplayController, I fixed the issue by adding a listener on UIKeyboardWillShowNotification, and setting [self setShowsCancelButton:NO animated:NO] there.

    In viewWillAppear::

    // Add keyboard observer:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillAppear:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    

    Then you create:

    - (void)keyboardWillAppear:(NSNotification *)notification
    {
        [YOUR-SEARCHBAR-HERE setShowsCancelButton:NO animated:NO];
    }
    

    Don't forget to add,

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    in viewWillDisappear:!

    Hope this helps!

    0 讨论(0)
  • 2020-12-24 07:23

    On iOS 13.0 and later, UISearchController has this property you can use:

    @property (nonatomic) BOOL automaticallyShowsCancelButton API_AVAILABLE(ios(13.0)); // Default YES
    
    0 讨论(0)
  • 2020-12-24 07:24

    This seems to be a bug within Xcode. I submitted this error to Apple's bug reporting site, and they've followed up asking for more sample code and use-cases.

    Thanks everyone for your attempt at solving this problem.

    0 讨论(0)
提交回复
热议问题