So I'm trying to make the "Cancel" button not show:

- (void)didPresentSearchController:(UISearchController *)searchController { searchController.searchBar.showsCancelButton = NO; } 

When you click on the text box UISearchBar button at the moment it appears and disappears.

I tried to do the same in the method that is called a little earlier:

 - (void)willPresentSearchController:(UISearchController *)searchController { searchController.searchBar.showsCancelButton = NO; } 

To no avail, the didPresentSearchController property showsCancelButton comes YES . Because of this, the button appears at the moment and disappears, after setting the property to NO . What is the problem?

    1 answer 1

    knowledgeable people advise UISearchBar to subclass and remove the button in layoutSubviews (taken from here )

     -(void)layoutSubviews{ [super layoutSubviews]; [self setShowsCancelButton:NO animated:NO]; } 

    if, let's say you have your own MySearchController subclass and have your own subclass for the MySearchBar bar, something like this will look like (I haven't tried it myself)

     @interface MySearchController () @property (nonatomic) MySearchBar *myBar; @end @implementation MySearchController - (UISearchBar*)searchBar { if(!_myBar) { _myBar = [MySearchBar new]; } return _myBar; } @end 
    • How then to override the searchBar of searchController? - Gikas
    • The e-file says that it is necessary to inherit and replace the bar with your own UISearchController> UISearchBar, subclass UISearchController - Max Mikheyenko
    • I can not redefine the property. Could you tell me exactly how to do this? - Gikas
    • added to the answer (I did not try to launch it myself) - Max Mikheyenko
    • Unfortunately not working. Getter Never Called - Gikas