UISearchController的searchBar不能填满整个宽度 [英] UISearchController's searchBar doesn't fill full width
问题描述
编辑:我正在使用自动版式.
I am using Autolayout.
我在使用UISearchController的searchBar填充屏幕的整个宽度时遇到问题.我的设置就像这样:
I have a problem filling the full width of my screen with a UISearchController's searchBar. My setup is like so :
- 我有一个UIViewController.在内部,我将所有元素分开.有一个UITableView显示结果.
- 由于searchBar与状态栏重叠时遇到了麻烦,因此我创建了一个普通的UIView,它具有约束,因此它位于状态栏下并填充了UIViewController的整个宽度.这是我在这里看到过很多次的方法.
- 最后,我创建UISearchController并将其添加为"wrapperView"的子视图
这是结果:
对我来说,由于左侧的那一侧似乎不像另一侧那么宽,所以searchBar要么没有正确地填充容器,要么没有正确地居中.
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:
这是我进行设置的代码:
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.
推荐答案
显然,只有模拟器无法正确显示搜索栏.我不知道这是否是模拟器的错误,但在设备上可以正常使用!因此,我会将这个答案标记为正确的答案,并希望它将为其他人提供很好的参考!干杯!
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!
这篇关于UISearchController的searchBar不能填满整个宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!