I want to hide the Navigation Bar of my page when the user start editing on the searchbar, I also want to show a cancel button.
It is done but my cancel button is not accessible when I bring up the UISearchBar
Thanks to All.
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
self.navigationController.navigationBar.hidden=TRUE;
CGRect r=self.view.frame;
r.origin.y=-44;
r.size.height+=44;
self.view.frame=r;
searchBar.showsCancelButton=TRUE;
}
You need to use UISearchDisplayController, that will hide navigation bar automatically for you. Here is the code,
You will have to implement protocols UISearchBarDelegate, UISearchDisplayDelegate, UITableViewDelegate, UITableViewDataSource in .h file.
also _searchBar & searchDisplayController are variables defined in .h file.
Swift 4:
Below 2 lines of code worked for me to show cancel button:
Calling inside
searchBarTextDidBeginEditing()
method will show cancel button after clicking on search bar.Objective C
Swift
Use this code to show/Hide the cancel Button in
SearchBar
As user will start the Editing in SearchBar SHow the Cancel Button.
Below methods will only be accessible if you have set the delegate of
SearchBar
properly.Hide The
CancelButton
as User Click on Cancel ButtonUse this code
Use this code for showing
cancel
button. It will show button when you will enter text insearchBar
.