iOS 11 Searchcontroller跳转到屏幕顶部

mar*_*010 7 ios uisearchcontroller

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

self.definesPresentationContext  = YES; 
tableHeaderView.clipsToBounds = YES; 
etc 
Run Code Online (Sandbox Code Playgroud)

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

还有其他选择吗?

Xcode 9 GM与iOS 11(Xcode 9 GM与iOS 10.x一起运行正常)

小智 1

首先需要添加 UISearchControllerDelegate

然后设置你的代表

self.searchController.delegate=self;
Run Code Online (Sandbox Code Playgroud)

放置一个新视图来保存您的 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
}
Run Code Online (Sandbox Code Playgroud)

然后添加这些委托方法

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

这对我有用,我希望你能成功

  • 我能够获取此代码来调整搜索栏的宽度,但我的搜索栏仍然跳出表格视图的顶部并转到屏幕的顶部边缘。如果我尝试在 didPresentSearchController: 中将其向下移动,那么尽管给了它很大的高度或更大的 Y 位置,它仍然会开始被奇怪地切断。 (2认同)