注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)
答案 0 :(得分:15)
经过一些试验和错误后,我有以下工作解决方案:
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,并且还要处理所有必需的委托方法,但就搜索栏而言,这对我有用。如果您有任何问题,请发表评论。
答案 1 :(得分:-1)
试试这个:
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)