Ios – Why the UISearchBar`s frame is changed by the UISearchDisplayController

iosuisearchbaruisearchbardelegateuisearchbardisplaycontroluisearchdisplaycontroller

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.
enter image description here

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?
enter image description here

when I cancel the UISearchDisplayController it is like this :
enter image description here

why the frame is changed? The width is changed from 320-44 to 320 by the UISearchDisplayController?
thanks.

Best Answer

UISearchDisplayController expands the search bar to the width of its superview. The simplest solution that I have found is to place the search bar inside another UIView that has the width I am looking for.

Related Topic