【发布时间】:2015-07-08 04:26:51
【问题描述】:
注意 1:这个问题涉及在它更新的表视图之外添加 UISearchController 的搜索栏 - 不是作为表视图的标题。
注意 2:一些试验和错误使我找到了解决方案。请在下方查看我的answer。
我是 iOS 开发新手,正在努力使用 UISearchController 类。我有一个视图控制器,在我的视图控制器的视图中,我计划在表格视图上方有一个搜索栏。我希望搜索栏链接到 UISearchController。由于界面生成器不附带 UISearchController,我以编程方式添加控制器。实例化 UISearchController 后,我尝试以编程方式将搜索控制器的搜索栏添加到我的视图中,但没有成功。我尝试设置搜索栏的框架并为其提供自动布局约束,但两种方法都对我不起作用(即,当我运行应用程序时,什么都没有出现)。这是我尝试过的最新代码:
let searchController = UISearchController(searchResultsController: nil)
// Set the search bar's frame
searchController.searchBar.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50)
// Constraint to pin the search bar to the top of the view
let topConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(searchController.searchBar)
self.view.addConstraint(topConstraint)
任何帮助将不胜感激!谢谢!
编辑: 仅使用其中一个设置搜索栏的框架或给它自动布局约束(而不是我最初尝试的组合)似乎一开始可以工作,但在点击之后正如 Dwight 所指出的那样,您将在搜索栏中遇到问题。我已经留下了这些案例的代码,以防与您当前拥有的代码进行比较会有所帮助,但对于一个可行的解决方案,请参阅下面的答案。
使用自动布局约束:
let searchController = UISearchController(searchResultsController: nil)
let topConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: (statusBarHeight + navigationBarHeight!))
let leftConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0)
let rightConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0)
let heightConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44)
searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false)
searchController.searchBar.addConstraint(heightConstraint)
self.view.addSubview(searchController.searchBar)
self.view.addConstraints([topConstraint, leftConstraint, rightConstraint])
我已将 topConstraint 常量设置为状态栏的高度加上导航栏的高度,因为我的视图控制器嵌入在导航控制器中。
调整框架:
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.frame = CGRect(x: 0, y: (statusBarHeight + navigationBarHeight!), width: self.view.frame.size.width, height: 44)
self.view.addSubview(searchController.searchBar)
【问题讨论】:
-
@Anbu.Karthik 感谢您的链接!第一个似乎只是我想要的,需要更多的界面构建器参与,所以我会仔细看看。我第二次使用 UISearchDisplayController,我认为它在 iOS 8 中已被弃用。
-
我遇到了和你一样的问题,但是在用户点击搜索栏后我遇到了问题。使用 frame 选项,搜索栏会向上滑出视图,并且 tableview 会显示但位置不正确。使用约束选项时,应用程序会因“视图层次结构未为约束做好准备:”错误而崩溃。您是否也遇到过这些问题?
-
@Dwight - 谢谢。你是对的 - 编辑 1 中的解决方案都不起作用。我已经确认了您遇到的相同问题。请参阅编辑2!这对我来说似乎效果很好。让我知道这是否适合您!
-
@kcstricks - 哇,非常感谢您的回复,我认为这将是一个失败的原因。编辑 2 工作出色,干得好!用 UISearchController 来获得 UISearchDisplayController 的功能真是太痛苦了!您绝对应该发布编辑 2 作为此问题的答案以帮助其他人。再次感谢!
标签: ios swift uisearchbar uisearchcontroller programmatically-created