ios - 以编程方式显示 UISearchController 的搜索栏

标签 ios swift uisearchbar uisearchcontroller programmatically-created

注意 1:此问题涉及在它更新的 TableView 之外添加 UISearchController 的搜索栏 - 而不是作为 TableView 的标题。

注意 2: 经过反复试验,我找到了解决方案。请看我的answer下面。

我是 iOS 开发的新手,正在努力使用 UISearchController 类。我有一个 View Controller ,在我的 View Controller 的 View 中,我计划在 TableView 上方有一个搜索栏。我希望搜索栏链接到 UISearchController。由于界面构建器不附带 UISearchController,因此我以编程方式添加 Controller 。在实例化 UISearchController 之后,我尝试以编程方式将搜索 Controller 的搜索栏添加到我的 View 中,但没有成功。我试过设置搜索栏的框架并给它自动布局约束,但是这两种方法都对我不起作用(即当我运行应用程序时,什么也没有出现)。这是我尝试过的最新代码:

    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 常量设置为状态栏的高度加上导航栏的高度,因为我的 View Controller 嵌入在导航 Controller 中。

调整框架:

    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)

最佳答案

经过反复试验,我得到了以下可行的解决方案:

  1. 打开 Main.storyboard 并向 View Controller 添加一个高度为 44(UISearchBar 的默认高度)的 UIView 和一个 UITableView。设置自动布局约束,以便将 UIView 固定到两侧和顶部布局指南(使用顶部布局指南可确保无论您的 View Controller 是否嵌入导航 Controller 中,这都有效)并将 TableView 固定到两侧,底部布局指南,其顶部固定到 UIView 的底部。
  2. 在您的 ViewController.swift 中将 UIView 和 UITableView 作为 IBOutlets 连接起来。在我的项目中,我将它们称为 topView 和 tableView。
  3. 在 ViewController.swift 的顶部声明一个搜索 Controller :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
    

应该是这样的!当然,您需要使您的 View Controller 成为 UITableViewDatasource、UITableViewDelegate、UISearchBarDelegate、UISearchControllerDelegate 和 UISearchResultsUpdating 并处理所有必需的委托(delegate)方法,但就搜索栏而言,这对我有用。如果您有任何问题,请发表评论。

关于ios - 以编程方式显示 UISearchController 的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283523/

相关文章:

objective-c - 移除 UISearchBar 的内阴影

objective-c - iOS:卸载和弹出 View Controller

ios - 将 UIScrollView 添加到 CCSprite

iphone - UISearchbar 键盘搜索按钮操作

swift - 为什么我的 Playground 在 NSRange() 上崩溃

ios - 向 UIAlert 添加操作以引导用户进行设置

ios - 如何触发 UISearchBar 委托(delegate)单次?

iphone - 如何使用 "OR"和 "AND"创建一个 compoundPredicate

ios - UITableViewCell 内容未填满整个单元格

swift - fatal error : Only BidirectionalCollections can be advanced by a negative amount