UISearchController:searchBar和scopeBar在第一个触摸事件上重叠

我正在尝试使用Apple最新的UISearchController实现一个简单的search栏。 但是,我似乎无法让它正常工作,如果我使用search栏的范围栏来获得筛选器的select。

范围栏总是显示我可以住在哪里 在启动范围栏总是显示

但在第一次触摸事件时,search栏和范围栏会重叠。 搜索栏和范围栏折叠

我使用Apple的TableView示例代码应用程序,但它没有改变任何东西。

- (void)viewDidLoad { [super viewDidLoad]; _resultsTableController = [[APLResultsTableController alloc] init]; _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController]; self.searchController.searchResultsUpdater = self; [self.searchController.searchBar sizeToFit]; self.searchController.searchBar.showsScopeBar = YES; [self.searchController.searchBar setScopeButtonTitles:@[@"First",@"Second"]]; self.tableView.tableHeaderView = self.searchController.searchBar; // we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables self.resultsTableController.tableView.delegate = self; self.searchController.delegate = self; self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES self.searchController.searchBar.delegate = self; // so we can monitor text changes + others // Search is now just presenting a view controller. As such, normal view controller // presentation semantics apply. Namely that presentation will walk up the view controller // hierarchy until it finds the root view controller or one that defines a presentation context. // self.definesPresentationContext = YES; // know where you want UISearchController to be displayed 

}

我也实现了search栏的委托方法 ,但它只是切换重叠的时间。

有没有人能够实现一个search栏使用iOS 8的UISearchController和使用searchBar的范围栏?

提前致谢

我花了三天的时间研究同样的问题,经过我可以find的所有post,最后在苹果开发论坛上find了解决scheme,所以尽pipe我会发布更新来帮助遇到此问题的下一个人。

解决方法是确保属于UISearchController的search栏上的showsScopeBar属性设置为false。 即快速UISearchCOntroller.searchBar.showsScopeBar = false。

这个问题似乎是,苹果的devise意图是将showsScopeBar属性用于独立的search栏,而不是由UISearchController控制的search栏。 不幸的是,这不是在类文档中提出的。 我认为糟糕的devise决定,但它是什么。

讨论是在以下线程https://devforums.apple.com/thread/235803

祝你好运。

这似乎是一个已知的缺陷。 有几个radr条目,如http://www.openradar.me/20702394引用类似的问题,并通过使用sizeToFit()

他们build议的解决方法是有效的,但只有在viewDidLayoutSubviews中应用它时才起作用。 即在所有的意见被布置之后。

 override func viewDidLayoutSubviews() { self.searchController.searchBar.sizeToFit() } 

更新为swift 3:

 searchController.searchBar.showsScopeBar = false 

swift更新4:

看来,在迅速的4和IOS 11search栏变了。 使用上面显示的方法,在某些情况下,范围栏将位于search栏内。

 if #available(iOS 11.0, *) { navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false } else { tableView.tableHeaderView = searchController.searchBar } 

这为我修好了。 如果使用ios 10,我使用上面显示的表单。 如果ios 11可用,我更改我的技术并将导航视图控制器设置为search控制器。 你会注意到它看起来和ios 10完全一样