2
votes

Background: I have a UIViewController which has a UITableView added programmatically. At the top of the UITableView I have a tablHeaderView in which I have placed a UIView. This UIView has a UISearchBar and a segmentedControl. The idea being: that a user can sort the UITableView by some basic categories such as 'Date/Time' or 'Location' etc. They can also search by an item in the programme.

Problem: When I tap the search bar it resizes (which I don't want) and then when I cancel the search it resizes again and stays there until the UIViewController is exited and loaded again.

Screenshot with first screen looking ok, but when you tap into the search bar it resizes and looks odd.  You then cancel the search to get rid of it and it resizes again, taking up all of the tableHeaderView

Code:

-(void)loadTableView
{
    usableSpace = [[UIScreen mainScreen] applicationFrame];
    usableWidth = usableSpace.size.width;
    usableHeight = usableSpace.size.height;

    _tableView = [[UITableView alloc] init];
    [_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    [self.view addSubview:_tableView];

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
    _searchBar.showsCancelButton = YES;

    NSLog(@"searchBar height = %fl", _searchBar.frame.size.height);
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Date/Time", @"Location", @"Speaker", nil]];
    segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
    [segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
    [headerView addSubview:_searchBar];
    [headerView addSubview:segmentedControl];
    _tableView.tableHeaderView = headerView;

    [self.view addSubview:_tableView];
    self.tableView = _tableView;

}

What I have tried: I have tried setting the size of the SearchBar, not setting its size. Not having it in a UIView in the tableHeaderView, instead just having it there on its own.

Why does it resize at all and how can I get it to stop?

EDIT: I have just tried the following: In storyboard (where the UIViewController was originally created) I have selected the UIVC in question and in attributes inspector I deselected 'Under Top Bars' and 'Under Bottom Bars' and this appears to have fixed the first part of the animation problem. Now, when I tap in the search bar, the search becomes active but the searchBar does NOT resize. However, when I cancel the searchBar the searchBar still animates and resizes as per the last image in my screenshot. What could be causing THAT resizing?

2
I have the same problem and I try to use this solution. It works correctly. <stackoverflow.com/questions/21419992/…>Kiet Nguyen

2 Answers

2
votes

i've been banging my head on this for too freakin long... props to this dude here for the clues.

if you want your UISearchBar to be in the header along with other views and to scroll with the header instead of sticking at the top, then you've got to remove it and re-add it after the search completes. to be clear, build your header like you already do and then throw this in there as well to handle the screwy animations.

-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    [self.mySearchBar removeFromSuperview];
    self.mySearchBar.frame = CGRectMake(0, 0, 320, 44);
    [self.table.tableHeaderView addSubview:self.mySearchBar];
}

As Timothy Moose notes here, "It seems that UISearchDisplayController makes an undocumented assumption that the search bar is the only content of the header view"

...yay apple.

0
votes

Ok, after hours of reading up and trying different things I think I have found out how to have a UISearchBar where I want it and it seems to work ok. I believe the problem was to do with either having multiple views in the tableHeaderView OR the UISearchBar did not like being with another view inside a 'containing' view.

Here is the code I ended up with that allowed me to have a segmentedControl at the top of my UITableView AND a UISearchBar which actually sat at the top of my UIView (to which the UITableView was added).

-(void)loadTableView
{
    usableSpace = [[UIScreen mainScreen] applicationFrame];
    usableWidth = usableSpace.size.width;
    usableHeight = usableSpace.size.height;

    _tableView = [[UITableView alloc] init];
    [_tableView setFrame:CGRectMake(0,0,usableWidth, usableHeight)];
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    [self.view addSubview:_tableView];

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, usableWidth, 44)];
    [_searchBar setShowsCancelButton:YES];

    NSLog(@"searchBar height = %fl", _searchBar.frame.size.height);
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Title", @"Date/Time", @"Speaker", nil]];
    segmentedControl.frame = CGRectMake(0, (_searchBar.frame.size.height), usableWidth, (usableHeight * 0.075));
    segmentedControl.selectedSegmentIndex = 0;
    [segmentedControl addTarget:self action:@selector(sortList) forControlEvents:UIControlEventValueChanged];

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, usableWidth, (usableHeight * 0.15))];
    [headerView addSubview:segmentedControl];
    _tableView.tableHeaderView = headerView;

    [self.view addSubview:_tableView];
    [self.view addSubview:_searchBar];
    self.tableView = _tableView;
}

This meant that I had: UIViewController with a UINavigationController, UISearchBar, UITableView. The UITableView had a segmentedConrol. The UISearchBar sits just under the UINavigationController and is always present. The UISearchBar sits at the top of the UITableViewController and scrolls with the UITableViewCells.

If anyone knows why the UISearchBar acted as it did in the question then I would be grateful if they could leave a comment. I know that this problem (or very similar) has been experienced by MANY people and I haven't found a definitive answer anywhere.