1
votes

EDIT: I am using Autolayout.

I have a problem filling the full width of my screen with a UISearchController's searchBar. My setup is like so :

  1. I have a UIViewController. Inside, I have all my elements separated. There is a UITableView displaying the results.
  2. Since I had trouble with the searchBar overlapping the status bar, I created a plain UIView that has constraints so that it lives under the status bar and fills the full width of the UIViewController. This is a method that I have seen numerous time here.
  3. Finally, I create the UISearchController and add it as a subview of "wrapperView"

Here's the result :

The blue is only to see to where the container view extends

To me, the searchBar either does not fill the container properly or it is not properly centered since the side on the left does not seem to be as wide as the other side.

Also, when the search box is active, everything is displayed fine like so:

The table also displays fine

Here is the code where I do my setup:

//Mark: - Outlets
@IBOutlet weak var wrapperView: UIView!
@IBOutlet weak var tableView: UITableView!

//Mark: - Properties
let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    /*!
    Sets the values of the searchController and it's behaviour
    */
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.delegate = self

    /*!
    Sets the values and behaviours of the tableView
    */
    tableView.delegate = self
    tableView.dataSource = self
    tableView.hidden = true

    definesPresentationContext = true
    searchController.searchBar.sizeToFit()
    searchController.searchBar.frame.size.width = self.view.frame.size.width
}

Also, my search logic works fine.

1
Are you using auto layout?Jeff Lewis
Yes I am. I have edited the original question to reflect that. To be honest, I tried setting constraints manually without success...Felix Brousseau

1 Answers

-1
votes

Apparently only the simulator is displaying the search bar badly. I don't know if this is a bug of simulator but on device it works fine! I will therefor mark this answer as the correct one and hopefully it will serve as a good reference for others! Cheers!