问题
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 :
- I have a UIViewController. Inside, I have all my elements separated. There is a UITableView displaying the results.
- 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.
- 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:
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!
来源:https://stackoverflow.com/questions/34776911/uisearchcontrollers-searchbar-doesnt-fill-full-width