iOS 11 Searchcontroller跳转到屏幕顶部 [英] iOS 11 Searchcontroller jumps top of screen
问题描述
具有与已发布的相同的问题(非答案工作..),我的表是固定宽度,并且在iOS 10.x中,搜索栏(在tableviewheader中)在键入时保持相同的大小。但是在iOS 11中,它会跳到屏幕顶部并在整个宽度上伸展。尝试了所有选项;
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..
任何其他选项?
Xcode 9 GM与iOS 11(Xcode 9 GM与iOS 10.x)
Xcode 9 GM with iOS 11 (Xcode 9 GM with iOS 10.x and it all works fine)
推荐答案
首先你需要添加UISearchControllerDelegate
First you need to add UISearchControllerDelegate
然后设置你的代表
self.searchController.delegate=self;
放置一个新视图,它将保存您的searchController
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
}
然后添加这些委托方法
- (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
这篇关于iOS 11 Searchcontroller跳转到屏幕顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!