Hide UISearchBar Cancel Button

后端 未结 11 834
夕颜
夕颜 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 06:57

    Had this problem when using the UISearchBar with UISearchController. I'm using my own cancel button, as the cancel button wasn't showing on iPad with showsCancelButton = YES, now it won't hide on iPhone with showsCancelButton = NO!

    The following worked for me.

    Set the delegate, and initial value:

    - (void)viewDidLoad
    {
        // ... 
        self.searchController.searchBar.showsCancelButton = NO;
        self.searchController.searchBar.delegate = self;
    }
    

    Reset showsCancelButton to NO 0.1s after the text bar begins editing.

    #pragma mark - UISearchBarDelegate
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            self.searchController.searchBar.showsCancelButton = NO;
        });
    }
    
    0 讨论(0)
  • 2020-12-24 07:02

    I managed to hide the "Cancel" button by subclassing UISearchBar and override this method:

    -(void)layoutSubviews{
        [super layoutSubviews];
        [self setShowsCancelButton:NO animated:NO];
    }
    
    0 讨论(0)
  • 2020-12-24 07:07

    If you want to avoid the subclassing, implement

    searchController.searchBar.showsCancelButton = false;
    

    in these two delegate methods (Do not forget to assign delegates):

    - (void)updateSearchResultsForSearchController:(UISearchController *)searchController
    
    - (void)didPresentSearchController:(UISearchController *)searchController
    

    The first one is called everytime you update the searchBar (Cancel button is visible by default) and the second one is for the first searchBar activation.

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

    Just based on issues I've had before have you tried setting it in:

    - (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
    

    I don't know how to ask this question in your question sorry if this is out of place.

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

    After UISearchDisplayController deprecated in iOS8, Apple give handle search presentation to UISearchControllerDelegate.

    so you can override searchBar to hide the Cancel button, like below :

    - (void)didPresentSearchController:(UISearchController *)searchController {
        [searchController.searchBar setShowsCancelButton:NO];
    }
    

    if you need hidden Cancel button from inactive state, you need set searchBar on init :

    search = [[UISearchController alloc] initWithSearchResultsController:nil];
    [search.searchBar setShowsCancelButton:NO];
    
    0 讨论(0)
  • 2020-12-24 07:17

    Similar to Nimrod's answer, you can also subclass UISearchDisplayController and implement the setActive:animated: method:

    - (void)setActive:(BOOL)visible animated:(BOOL)animated {
        [super setActive:visible animated:animated];
        self.searchBar.showsCancelButton = NO;
    }
    
    0 讨论(0)
提交回复
热议问题