[英]UISearchController weird behavior on dismissing it
I am adding UISearchController
searchBar to the controllers' view like this: self.view.addSubview(searchController.searchBar)
. 我将
UISearchController
searchBar添加到这样的控制器视图中: self.view.addSubview(searchController.searchBar)
。 The functionality is working perfectly fine except that upon selection of the tableview row the searchbar quickly moves down and reappears from the top. 该功能运行良好,只是在选择了表格视图行后,搜索栏迅速向下移动并从顶部重新出现。 I tried the following things, none of which worked out:
我尝试了以下操作,但都没有成功:
tableView.tableHeaderView = searchController.searchBar
instead of directly adding to the view tableView.tableHeaderView = searchController.searchBar
而不是直接添加到视图中 searchController.searchBar
to a separate view that I dragged to the controller setting up constraints on it. searchController.searchBar
添加到一个单独的视图中,我将其拖动到控制器上以对其设置约束。 Tried clipping to bounds both the newly created view and the searchBar. UINavigationViewController
and setting self.navigationItem.titleView = searchController.searchBar
. UINavigationViewController
并设置self.navigationItem.titleView = searchController.searchBar
。 I defined the frame of the searchBar, still nothing. Extend Edges
feature in the storyboard (Under top bars, etc.), but no selection worked out Extend Edges
中的“ Extend Edges
功能(在顶部栏等下),但未进行选择 Adding lines (to viewDIdLoad
): 添加行(到
viewDIdLoad
):
self.extendedLayoutIncludesOpaqueBars = true
self.definesPresentationContext = true
Any help would be greatly appreciated. 任何帮助将不胜感激。
如果使用情节提要,则可以通过选择视图控制器对其进行更改,然后在属性检查器中取消选择“调整滚动视图插图”。
After trying all of the suggestion and searching over the internet, it caught my eye that the working examples of the UISearchController
implementation are done in UITableViewController
, but I had UIViewController
with UITableViewDataSource
and UITableViewDelegate
protocols on it. 在尝试了所有建议并在Internet上搜索之后,我注意到
UISearchController
实现的工作示例是在UITableViewController
中完成的,但是我使用了带有UITableViewDataSource
和UITableViewDelegate
协议的UIViewController
。 Unfortunately, due to app architecture I was not able to directly have UITableViewController
, so I needed to restructure the app, so that it had UINavigationController
where I embeded the searchBar
in navigationItem.titleView
(and not set it as tableView.tableHeaderView
like they always do in various tutorials since I needed the searchBar
to be fixed, not hidden when we do scrolling) and it worked. 不幸的是,由于应用程序的架构,我无法直接拥有
UITableViewController
,所以我需要重新构建应用程序,所以它有UINavigationController
,我内嵌的searchBar
在navigationItem.titleView
(而不是将其设置为tableView.tableHeaderView
就像他们总是这样在各种教程中,因为我需要固定searchBar
,而不是在滚动时隐藏它,所以它可以工作。 Here is how the ultimate working app architecture looks like: 最终工作的应用程序架构如下所示:
The TrainingContainerViewController
has two Container View
s, in one we embed TrainingFilterTableViewController
that shows up the ultimate results of the autocomplete functionality (after clicking on an autocomplete row). TrainingContainerViewController
有两个Container View
,其中一个嵌入了TrainingFilterTableViewController
,它显示了自动完成功能的最终结果(单击自动完成行之后)。 Another Container View
embeds UINavigationController
(to the left) which, in turn, has TrainingSearchTableViewController
as its child. 另一个
Container View
嵌入UINavigationController
(在左侧),该UINavigationController
又将TrainingSearchTableViewController
作为其子级。
The code that sets up the UISearchController
and its searchBar
is located in the TrainingSearchTableViewController
s' viewDidLoad
and is the following: 设置
UISearchController
及其searchBar
的代码位于TrainingSearchTableViewController
的viewDidLoad
中,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
searchController.delegate = self
navigationItem.titleView = searchController.searchBar
tableView.hidden = true
...
}
Nothing else was needed to fix the bug in XCode 8.2.1
and Swift 2.3
, just change architecture, so that usage of UITableViewController
is possible in the app and use it instead of UIViewController
. 无需其他任何操作即可修复
XCode 8.2.1
和Swift 2.3
的错误,只需更改体系结构即可在应用程序中使用UITableViewController
并使用它代替UIViewController
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.