Search code examples
iosswiftuikituisearchbaruisearchcontroller

Preserve margins for UISearchBar


I'm trying to create UISearchController and place its search bar into container view. searchView is outlet. Here's my code:

    let searchController = UISearchController(searchResultsController: nil)
    self.searchController = searchController

    let searchBarBackground = UIImage.roundedImage(UIImage.image(with: #colorLiteral(red: 0.5568627451, green: 0.5568627451, blue: 0.5764705882, alpha: 0.2), size: size), cornerRadius: cornerRadius)
    searchController.searchBar.setSearchFieldBackgroundImage(searchBarBackground, for: .normal)
    searchController.searchBar.searchTextPositionAdjustment = UIOffsetMake(8.0, 0.0)
    searchController.searchBar.searchBarStyle = .minimal
    searchController.searchBar.barStyle = .black
    searchController.searchBar.delegate = self

    searchView.addSubview(searchController.searchBar)

I can't figure out how to make search bar to preserve margins from main view. When I'm trying to add constraints to container view and add search bar as subview, search bar doesn't respect trailing constraint.

enter image description here

Even manual setting of search bar frame equals to content view bounds in viewDidLayoutSubviews doesn't help. It looks OK only when I set leading and trailing constraints to 0 without respecting superview margins. But it has only 8pt margins from sides, I need more.

enter image description here

How can I achieve it? Maybe it's possible to change default margin of search bar? Thanks.

Screenshot from IB if needed (it's just constraints to margin with value -8 to compensate search bar internal margins):

enter image description here


Solution

  • I ended up with following solution: update search bar frame after every presenting and dismissing of search controller and in viewDidAppear(_:) for initial setup.

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        searchController?.searchBar.frame.size.width = searchView.bounds.width
    }
    
    func didPresentSearchController(_ searchController: UISearchController) {
        searchController.searchBar.frame.size.width = self.searchView.bounds.width
    }
    
    func didDismissSearchController(_ searchController: UISearchController) {
        searchController.searchBar.frame.size.width = self.searchView.bounds.width
    }