我遇到了一个让我头疼的问题。
在 Interface Builder 中,我有一个只包含一个表格 View 的场景。这个子类 UITableViewController 的 Controller 。我有另一个场景,顶部有一个高度为 50 的 View ,横跨 View 的宽度。下面是一个容器 View ,最终包含一个列表或 map 。 Controller 是 UIViewController 的子类。这是它在模拟器上的样子...
请注意,您在上图中看到的列表是一个完全不同的表和 View Controller 。搜索栏 Controller 用于按城市或 zip 搜索位置。搜索栏 Controller 的 TableView 覆盖了另一个列表。当从搜索表中选择一个位置时,该位置的区域被提供给另一个 Controller ,该 Controller 使用私有(private)网络服务检索兴趣点列表。
我正在调用以下方法将搜索栏添加到上述 View 中。
- (void)setupSearchView {
self.searchTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LocationSearchTable"];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchTableViewController];
self.searchController.searchResultsUpdater = self.searchTableViewController;
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.tag = 9189;
self.searchController.searchBar.placeholder = @"Search by city or ZIP code";
[self.searchView addSubview:self.searchController.searchBar];
[[self.searchView viewWithTag:9189] becomeFirstResponder];
self.searchController.hidesNavigationBarDuringPresentation = false;
self.searchController.dimsBackgroundDuringPresentation = true;
self.definesPresentationContext = true;
}
当我在文本字段中单击时,将 self.definesPresentationContext 设置为 true,搜索栏下降到导航栏的确切高度,在其上方留下一个大的空白区域。
如果 self.definesPresentationContext 为 false,则行为和结果不同。在这种情况下,当我单击搜索字段时,一切看起来都很好。但是,一旦我开始键入,导航 Controller 就会被白色覆盖。搜索栏不会改变位置。如果我使用 Reveal,我可以看到导航栏已被推回到堆栈中。这就是它的样子......
我错过了什么?
最佳答案
我遇到了同样的问题,请尝试在呈现的 viewController 的 viewDidLoad 中使用以下代码行。
self.extendedLayoutIncludesOpaqueBars = true
关于ios - UISearchBar 在操作文本字段时移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39503089/