Hide UISearchBar Cancel Button

后端 未结 11 871
夕颜
夕颜 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;
        });
    }
    

提交回复
热议问题