注意1: 此问题与在更新的表视图之外添加UISearchController的搜索栏有关,而不是作为表视图的标题。
注意2: 一些反复试验使我找到了解决方案。
我是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)
任何帮助将不胜感激!谢谢!
编辑: 仅使用一种设置搜索栏的框架,或者您给它设置自动布局约束(相对于我最初尝试的组合方式)一开始似乎可以工作,但是点击搜索栏后,您将遇到所指出的问题由德怀特。我将这些情况的代码保留下来,以防与您目前拥有的代码进行比较,但是对于有效的解决方案,请参见下面的答案。
使用自动布局约束:
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)
经过一番反复尝试后,我得到了以下可行的解决方案:
var searchController: UISearchController!
然后,在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,并且还要照顾所有必需的委托方法,但是就搜索栏而言,这对我来说是有用的。如果您有任何问题,请发表评论。