Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you want to do is pretty tough. There is no built-in hook to get at the cancel button.</p> <p>However, there are a couple of options if you are willing to jimmy open the hood.</p> <p>First off, UISearchBar is a UIView, and the Cancel button is also a view, which is added into the search bar as a subview, just as you would expect.</p> <p>I have experimented a little, and can tell you that when the button is onscreen it has a size of 48,30.</p> <p>So in viewWillAppear, you can do something like this:</p> <ol> <li><p>Find the cancel button view in [searchBar subviews] by looking for one with size 48,30. (There only seems to be one -- this could change...) You could be doubly careful and look for one that is in approximately the correct position (differs in landscape and portrait).</p></li> <li><p>Add a subview to the cancel button. </p></li> <li><p>The subview should be a UIControl (so that you can set enabled = NO, in order to make sure touch events get to the actual cancel button)</p></li> <li><p>It needs to have the right color and rounded corners; you will need to fudge the size for reasons I don't yet understand (55,30 seems to work)</p></li> <li><p>This will work if searchBar.showsCancelButton is always YES; if you want it to disappear when not editing the search string, you will need to find a hook to add the overlay each time the cancel button appears.</p></li> <li><p>As you can see, this is some ugly tinkering. Do it with eyes wide open.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload