UISearchController's searchBar doesn't fill full width

这一生的挚爱 提交于 2019-12-24 15:49:04

问题


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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!