iOS 11 Searchcontroller jumps top of screen

一笑奈何 提交于 2019-12-21 12:09:10

问题


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)


回答1:


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



来源:https://stackoverflow.com/questions/46221717/ios-11-searchcontroller-jumps-top-of-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!