Question

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.

Was it helpful?

Solution 2

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:

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    [searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
}

Another is UISearchDisplayController's delegate:

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
    [controller.searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
    [self.searchDisplayController.searchResultsTableView setDelegate:self];
}

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.

OTHER TIPS

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.

Call the delegate method

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    [controller.searchBar setFrame:CGRectMake(0,31, 320 , 43)];
    [self.searchDisplayController.searchResultsTableView setDelegate:self];
} 

You can handle the cancel button of searchBar using - (void)setShowsCancelButton:animated: If you do not want to show cancel button (as cancel button will change the frame of searchBar) just write this in delegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{   

    [searchBar setShowsCancelButton:NO animated:YES];
}

Updated:

An only possible solution seems to be finding the cancel button from SearchBar view hierarchy and hiding it.

for (UIView *possibleButton in searchBar.subviews)
{
    if ([possibleButton isKindOfClass:[UIButton class]])
    {
        UIButton *cancelButton = (UIButton*)possibleButton;
        cancelButton.hidden = YES;
        break;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top