I write UISearchBar in my TopBar.m like this:
_tempSearchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(44, 0, 320 - 44, 43)];
_tempSearchBar.barStyle=UIBarStyleDefault;
_tempSearchBar.placeholder=@"搜索";
[self addSubview:_tempSearchBar];
the result is like this, it is right.
and then I write UISearchDisplayController in another class like this:
_topBar.tempSearchBar.delegate = self;
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_topBar.tempSearchBar contentsController:self];
[_searchDisplayController setDelegate:self];
[_searchDisplayController setSearchResultsDataSource:self];
the UISearchBarDelegate is like this:
#pragma mark -
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[_searchDisplayController setActive:YES animated:YES];
}
when I click the UISearchBar , it show like this , the searchBar`s frame is changed.why?
when I cancel the UISearchDisplayController it is like this :
why the frame is changed? The width is changed from 320-44 to 320 by the UISearchDisplayController?
thanks.
The searchBar's frame is changed by the UIKit, so I changed the searchBar's frame back myself.
I changed the searchBar's frame in the below delegate.
One is UISearchBar's delegate:
Another is UISearchDisplayController's delegate:
It can work and I can get the right frame, but when I click the searchBar it will shake a little.
It is not the best way to do it, but it can work. Does anyone have a better method?
Update: I have debugged the UISearchBar and UISearchDisplayController for a few hours, but it has a little bug: When I endEditing the searchBar's width will become 320px, and then will become my width. I can not change the cancelButton's background color. So I wrote a custom SearchDisplayController, with a UISearchBar property and a UITableView property. It works well for me.