Hide UISearchBar Cancel Button

后端 未结 11 903
夕颜
夕颜 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: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!

提交回复
热议问题