简体   繁体   English

UISearchController在解雇它时有奇怪的行为

[英]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: 我尝试了以下操作,但都没有成功:

  1. Setting tableView.tableHeaderView = searchController.searchBar instead of directly adding to the view 设置tableView.tableHeaderView = searchController.searchBar而不是直接添加到视图中
  2. Adding 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. 尝试裁剪以限制新创建的视图和searchBar的边界。
  3. Embedding the controller in UINavigationViewController and setting self.navigationItem.titleView = searchController.searchBar . 将控制器嵌入UINavigationViewController并设置self.navigationItem.titleView = searchController.searchBar I defined the frame of the searchBar, still nothing. 我定义了searchBar的框架,仍然没有。
  4. Tried playing with Extend Edges feature in the storyboard (Under top bars, etc.), but no selection worked out 尝试使用情节Extend Edges中的“ Extend Edges功能(在顶部栏等下),但未进行选择
  5. 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中完成的,但是我使用了带有UITableViewDataSourceUITableViewDelegate协议的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 ,我内嵌的searchBarnavigationItem.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的代码位于TrainingSearchTableViewControllerviewDidLoad中,如下所示:

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.1Swift 2.3的错误,只需更改体系结构即可在应用程序中使用UITableViewController并使用它代替UIViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM