編輯:我正在使用Autolayout。UISearchController的searchBar不能填滿整個寬度
我有一個問題,用UISearchController的searchBar填充我的屏幕的整個寬度。我的設置是這樣的:
- 我有一個UIViewController。在裏面,我把所有的元素分開了。有一個UITableView顯示結果。
- 由於我在搜索欄重疊狀態欄時遇到了問題,我創建了一個普通的UIView,它具有約束條件,因此它位於狀態欄的下方並填充UIViewController的全部寬度。這是我在這裏看到無數次的方法。
- 最後,我創建了UISearchController並將其添加爲「wrapperView」
下面的一個子視圖結果如下:
The blue is only to see to where the container view extends
對我來說,搜索欄或者不正確填充容器或者它沒有正確居中,因爲左側看起來不像另一側那麼寬。
此外,當搜索框是活動的,一切都顯示精細,像這樣:
這裏就是我做我的設置代碼:
//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
}
而且,我的搜索邏輯工作正常。
您是否使用自動佈局? –
是的,我。我編輯了原來的問題來反映這一點。說實話,我嘗試手動設置約束沒有成功... –