我正在学习UISearchController,很多例子表明你创建了一个UISearchController并将searchController.searchBar放在tableHeaderView中。所以我在我的代码中做了这个,它看起来没问题,直到我开始输入,然后转到详细视图,然后返回并且崩溃时出现以下错误:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x61000007a780 "UIView:0x7f89e7f2df20.top"> and <NSLayoutYAxisAnchor:0x608000075dc0 "UINavigationBar:0x7f89e7c0cc10.bottom"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
我创建UISearchController的代码如下:
let searchController = UISearchController(searchResultsController: nil)
extendedLayoutIncludesOpaqueBars = true
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar // remove and it works
如果我删除了最后一行tableView.tableHeaderView = searchController.searchBar
,我不会遇到任何崩溃。有没有什么需要做才能让这个工作?我加了一个GIF动画来表明它失败了。