在 iOS 11 和 12 上,我无法找到让 searchBar 使用整个屏幕宽度的方法。
navigationItem.searchController = searchController
我可以使用以下方法添加更多边距:
let sb = searchController.searchBar
let selector = #selector(setter: UIScrollView.contentInset)
if sb.responds(to: selector) {
sb.perform(selector, with: UIEdgeInsets(top: 5, left: 0, bottom: 5, right: 35))
}
如果我尝试将 left 和 right 设置为 0...它只会使用默认布局。
编辑:
越来越近:
for tf in sb.subviews.first!.subviews where tf is UITextField {
tf.translatesAutoresizingMaskIntoConstraints = false
tf.widthAnchor.constraint(equalToConstant: width).isActive = true
tf.layoutIfNeeded()
}
现在我正在尝试找出一种应用约束的方法,以便当文本字段处于事件状态时,它将为取消按钮(UINavigationButton)腾出空间
最佳答案
你试过吗?
let width = UIScreen.main.bounds.size.width
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.widthAnchor.constraint(equalToConstant: width).isActive = true
这里搜索栏宽度将始终等于设备屏幕的整个宽度。
关于ios - 如何使navigationItem.searchController的searchBar使用屏幕的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54519968/