I have an UISearchBar
in the titleView
of navigation bar (it looks like the picture below) and I want make it works like the one in Safari browser.
I don't have enough reputation to post images, here's the link
First picture
Second picture
What I want to achieve are:
- In normal state, the Navigation Bar contains 2 left and right buttons, an
UISearchBar
and a clear button inside that search bar (looks like first picture with 2 addition buttons). - In search state, the
view
is replaced by anotherview
and when it dismissed, theview
back to original state. - Works both in iOS 6 and 7
I know this can be done by using UISearchDisplayController
but it doesn't work. Here's my code:
.h : implement TableView's DataSource/Delegate, UISearchDisplayDelegate, UISearchBarDelegate
.m
-(void) viewDidload
{
//Add left, right buttons
self.leftButton = [[UIBarButtonItem alloc] init];
[self.leftButton setStyle:UIBarButtonItemStylePlain];
[self.leftButton setTitle:@"Button"];
self.navigationItem.leftBarButtonItem = self.leftButton;
self.rightButton = [[UIBarButtonItem alloc] init];
[self.rightButton setStyle:UIBarButtonItemStylePlain];
[self.rightButton setTitle:@"Button"];
self.navigationItem.rightBarButtonItem = self.rightButton;
self.searchBar = [[UISearchBar alloc] initWithFrame:self.navigationController.navigationBar.bounds];
if (DEVICE_IS_IOS7) {
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}
self.navigationItem.titleView = self.searchBar;
self.searchBar.delegate = self;
//----------------
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
}
/*
table implementation
*/
In iOS 7, the view
of UISearchDisplayController
(the grey one) didn't cover the main view and resultTable
did not reload data, even it was able to filter the search string. (I had to use KVO to display it)
In iOS 6, the grey thing covers all the screen and keyboard didn't show up (it disappeared immediately).
Can anybody help ? Thank you.