iOS 11 Searchcontroller跳转到屏幕顶部

时间:2017-09-14 14:27:00

标签: ios uisearchcontroller

具有与已发布的相同的问题(非答案工作..),我的表是固定宽度,并且在iOS 10.x中,搜索栏(在tableviewheader中)在键入时保持相同的大小。但是在iOS 11中,它跳转到屏幕顶部并在整个宽度上伸展。尝试了所有选项;

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

但似乎没有任何改变..它仍然跳到顶部全宽..

还有其他选择吗?

使用iOS 11的Xcode 9 GM(Xcode 9 GM和iOS 10.x,一切正常)

1 个答案:

答案 0 :(得分:1)

首先,您需要添加UISearchControllerDelegate

然后设置你的代表

self.searchController.delegate=self;

放置一个包含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
}

然后添加这些委托方法

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

这对我有用,我希望你能让它发挥作用