Display UISearchController's searchbar programmatically

核能气质少年 提交于 2019-11-29 04:22:15

After some more trial and error, I have the following working solution:

  1. Open up Main.storyboard and add to your view controller a UIView with height 44 (default height of a UISearchBar) and a UITableView. Set the autolayout constraints so that the UIView is pinned to the two sides and to the top layout guide (using the top layout guide ensures this will work whether or not your view controller is embedded in a navigation controller) and the table view is pinned to the two sides, the bottom layout guide, and its top pinned to the bottom of the UIView.
  2. Connect the UIView and UITableView as IBOutlets in your ViewController.swift. In my project, I called them topView and tableView.
  3. Declare a search controller at the top of your ViewController.swift: var searchController: UISearchController!
  4. Then, in viewDidLoad(), you will need:

    // 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
    

This should be it! Of course, you'll need to make your view controller a UITableViewDatasource, UITableViewDelegate, UISearchBarDelegate, UISearchControllerDelegate, and UISearchResultsUpdating and take care of all required delegate methods as well, but as far as the search bar goes, this has worked for me. Please comment if you have any issues.

try this:

        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)

or you can just do this:

    searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(false)   
->  searchController.searchBar.setTranslatesAutoresizingMaskIntoConstraints(true)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!