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

kcs*_*cks 8 uisearchbar ios programmatically-created swift uisearchcontroller

注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)
Run Code Online (Sandbox Code Playgroud)

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

编辑:只使用其中一个设置搜索栏的框架或你给它自动布局约束(而不是我最初尝试的组合)似乎首先工作,但点击搜索栏后你会遇到问题指出由德怀特.我已经离开了这些案例的代码,以防它与您现有的内容进行比较,但是对于一个有效的解决方案,请参阅下面的答案.

使用自动布局约束:

    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])
Run Code Online (Sandbox Code Playgroud)

我将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)
Run Code Online (Sandbox Code Playgroud)

kcs*_*cks 15

经过一些试验和错误后,我有以下工作解决方案:

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

这应该是它!当然,您需要将视图控制器设置为UITableViewDatasource,UITableViewDelegate,UISearchBarDelegate,UISearchControllerDelegate和UISearchResultsUpdating,并且还要处理所有必需的委托方法,但就搜索栏而言,这对我有用.如果您有任何问题,请评论.

  • "//由于某种原因,搜索栏将延伸到视图左侧".它是这样做的,因为在viewDidLoad()中,视图的帧尚未正确计算,尽管您的搜索栏获得了一个比它应该更大的错误帧.如果你想正确地制作东西,可以在viewDidAppear中或在视图完成布局后制作sizeToFit(). (3认同)
  • 当我执行此操作并点击搜索栏时,搜索栏会向下跳跃 80 点。当我取消搜索时,它会回到原来的位置。有谁知道如何解决这一问题? (2认同)