3

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.

cloosen
  • 993
  • 7
  • 17

4 Answers4

12

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.

Sean Lynch
  • 223
  • 1
  • 4
  • Struggled with this for hours. Wrapped it in a UIView and bam! Always wrap it people! – lionpants Mar 16 '15 at 15:14
  • A Great answer! My complete code: `UIView *searchContainerView = [UIView new]; [searchContainerView addSubview:self.searchController.searchBar]; UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchContainerView]; self.navigationItem.rightBarButtonItem = searchBarItem;GRect bounds = self.navigationController.view.frame; bounds= CGRectMake(120, 0, bounds.size.width-120, 44); [searchContainerView setFrame:bounds];` – drpawelo Jul 01 '15 at 13:07
5

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.

Tim
  • 597
  • 1
  • 5
  • 15
cloosen
  • 993
  • 7
  • 17
0

Call the delegate method

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    [controller.searchBar setFrame:CGRectMake(0,31, 320 , 43)];
    [self.searchDisplayController.searchResultsTableView setDelegate:self];
} 
Gábor Imre
  • 5,899
  • 2
  • 35
  • 48
-1

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;
    }
}
Animesh
  • 1,020
  • 9
  • 8
  • thank you for your answer , but it does not work ... the cancel button also be showed although I setShowsCancelButton:NO . and the searchBar`s frame also be changed – cloosen Oct 16 '12 at 09:47
  • thank you very much , I set the cancelButton hidden in the beginEditing delegate and the endEditing delegate , but the cancelButton will show a shink and then hidden . I think the UISearchDisplayController has some bugs(may be features) , so I write a custom SearchDisplayController , it works well for me . Once again thanked – cloosen Oct 17 '12 at 09:26
  • you can do it in ViewDidLoad itself or where-ever you have created the SearchBar before creating SearchDisplayController. – Animesh Oct 17 '12 at 13:35