以编程方式显示UISearchController的search栏

注1:这个问题涉及在它更新的表视图之外添加一个UISearchController的search栏 – 而不是表视图的标题。

注2:一些试错导致我find了解决办法。 请参阅下面的答案 。

我是iOS开发新手,正在努力使用UISearchController类。 我有一个视图控制器,在我的视图控制器的视图中,我打算在表视图上方有一个search栏。 我想要search栏被链接到一个UISearchController。 由于界面生成器没有附带UISearchController,我正在以编程方式添加控制器。 实例化一个UISearchController后,我试图以编程方式将search控制器的search栏添加到我的视图,但没有成功。 我试图设置search栏的框架,并给它自动布局约束,但这两种方法都没有为我工作(即当我运行应用程序,什么都没有出现)。 这是我尝试过的最新代码:

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) 

任何帮助将不胜感激! 谢谢!

编辑:只使用其中一个设置search栏的框架,或者你给它自动布局约束(而不是像我最初尝试的组合),似乎开始工作,但在点击search栏后,你会有指出问题由德怀特。 我已经留下了这些案例的代码,以便比较你现在所拥有的东西,但是对于一个可行的解决scheme,请参阅下面的答案。

使用自动布局约束:

  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设置为状态栏的高度加上导航栏的高度,因为我的视图控制器被embedded到导航控制器中。

调整框架:

  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) 

经过一些更多的尝试和错误,我有以下的工作解决scheme:

  1. 打开Main.storyboard并添加到你的视图控制器UIView与高度44(UISearchBar的默认高度)和UITableView。 设置自动布局约束,以便将UIView固定到双面和顶部布局指南(使用顶部布局指南确保无论您的视图控制器是否embedded到导航控制器中都能正常工作),并将表视图固定到双方,底部的布局指南,其顶部固定到UIView的底部。
  2. 在ViewController.swift中将UIView和UITableView作为IBOutlets连接起来。 在我的项目中,我称他们为topView和tableView。
  3. 在ViewController.swift的顶部声明一个search控制器: var searchController: UISearchController!
  4. 然后,在viewDidLoad()中,您将需要:

     // Initialize and set up the search controller self.searchController = UISearchController(searchResultsController: nil) self.searchController.searchResultsUpdater = self self.searchController.dimsBackgroundDuringPresentation = false // Optional self.searchController.searchBar.delegate = self // Add the search bar as a subview of the UIView you added above the table view self.topView.addSubview(self.searchController.searchBar) // Call sizeToFit() on the search bar so it fits nicely in the UIView self.searchController.searchBar.sizeToFit() // For some reason, the search bar will extend outside the view to the left after calling sizeToFit. This next line corrects this. self.searchController.searchBar.frame.size.width = self.view.frame.size.width 

这应该是它! 当然,你需要使你的视图控制器成为UITableViewDatasource,UITableViewDelegate,UISearchBarDelegate,UISearchControllerDelegate和UISearchResultsUpdating,并且照顾所有必需的委托方法,但是就search栏而言,这对我来说是有效的。 如果您有任何问题,请发表评论。

尝试这个:

  let searchController = UISearchController(searchResultsController: nil).searchBar // Set the search bar's frame searchController.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, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0) // searchController.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(searchController) // self.presentViewController(searchController, animated: true, completion: nil) self.view.addConstraint(topConstraint) 

或者你可以这样做:

  searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false) -> searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(true)