Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 Searchcontroller jumps top of screen

having the same issue as already posted (non of the answers works..), my table is a fixed width, and in iOS 10.x the searchbar (which is in the tableviewheader), stays the same size when typing. However in iOS 11, it jumps to the top of the screen and gets stretched out over the total width. All options tried;

self.definesPresentationContext  = YES; 
tableHeaderView.clipsToBounds = YES; 
etc 

but nothing seems to change.. it still jumps to the top full width..

Any other options?

Xcode 9 GM with iOS 11 (Xcode 9 GM with iOS 10.x and it all works fine)

like image 540
martin010 Avatar asked Sep 14 '17 14:09

martin010


1 Answers

First you need to add UISearchControllerDelegate

then set your delegate

self.searchController.delegate=self;

Place a new view which will hold your searchController

-(void) viewDidLoad{
    UIView *searchContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.searchTable.frame.size.width, 30)];
    searchContainerView.layer.masksToBounds=YES;
    searchContainerView.clipsToBounds=YES;
    self.tableView.tableHeaderView=searchContainerView;
    self.searchController.searchBar.layer.masksToBounds=YES;
    self.searchController.searchBar.clipsToBounds=YES;
    [searchContainerView addSubview:self.searchController.searchBar];
    [self.searchController.searchBar setFrame:CGRectMake(0, 0, 300, 30)];//Make sure that your header view and searchcontroller size is same
}

Then add these delegate methods

- (void)didPresentSearchController:(UISearchController *)searchController{
    [searchController.searchBar setFrame:CGRectMake(0, 0, 300, 30)];
}
- (void)didDismissSearchController:(UISearchController *)searchController{
    [searchController.searchBar setFrame:CGRectMake(0, 0, 300, 30)];
}

That worked for me, I hope you will make it work

like image 62
BaranBerk Avatar answered Dec 09 '22 16:12

BaranBerk