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.
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.
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):