How to connect a UISearchBar storyboard to a programmatically created UISearchController?

I previously had searchController.searchBar in the header of my UITableView , but I wanted to add buttons to change which array is displayed. Now that I have added the UISearchBar in my storyboard, I am trying to figure out how to connect it to my UISearchController so that I can correctly update the results. However, even in the latest Xcode, we still have a UIDisplayController in IB.

Two ways that I think can solve this are either a way to connect them, and I don’t know this, or just use nativeBear native for the searchController and move it to the right place. I hesitate to try the latter because of the advantages of AutoLayout for calibration.

+8
ios xcode uitableview swift
source share
1 answer

I always just put the substitute view in the interface builder file, which has 44 growth points. You can then add SearchController.view as a child of this container view.

  @IBOutlet weak var searchContainerView: UIView! fileprivate lazy var searchController: UISearchController = { let contactSearchViewController = self.storyboard?.instantiateViewController(withIdentifier: String(describing: ContactSearchViewController.self)) as! ContactSearchViewController contactSearchViewController.delegate = self contactSearchViewController.datasource = self let searchController = UISearchController(searchResultsController: contactSearchViewController) searchController.searchResultsUpdater = contactSearchViewController searchController.dimsBackgroundDuringPresentation = false searchController.definesPresentationContext = true return searchController }() override func viewDidLoad() { super.viewDidLoad() searchContainerView.addSubview(searchController.searchBar) let attributes: [NSLayoutAttribute] = [.top, .bottom, . left, .right] NSLayoutConstraint.activate(attributes.map{NSLayoutConstraint(item: self.searchController.searchBar, attribute: $0, relatedBy: .equal, toItem: self.searchContainerView, attribute: $0, multiplier: 1, constant: 0)}) } 
0
source share

All Articles