【发布时间】:2016-12-20 16:24:19
【问题描述】:
我的 UIStackView 中有一个带有按钮的 UISearchBar,它是一种导航栏。这是它通常的样子: 但是当它成为第一响应者时,UISearchBar 的宽度变为 0,它看起来像这样:
现在我唯一的限制是右侧 UIButton 的宽度为 44pt,并且整个堆栈视图固定在屏幕的顶部和侧面。这是控制台打印的内容:
我对@987654329@ 不是很流利,但显然有些东西与按钮上的 44pt 宽度限制相冲突。为什么UISearchbar的宽度变成0了?
这是我的代码:
override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
if presentationStyle == .expanded {
searchController.searchBar.becomeFirstResponder()
searchController.searchBar.tintColor = UIColor.white
}
}
func didDismissSearchController(_ searchController: UISearchController) {
requestPresentationStyle(.compact)
}
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
}
func configureSearchController() {
searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
searchController.searchBar.sizeToFit()
searchController.searchBar.isTranslucent = false
searchController.searchBar.delegate = self
searchController.delegate = self
searchController.searchBar.barTintColor = addButton.backgroundColor
stackView.insertArrangedSubview(searchController.searchBar, at: 0)
//This removes the hairline
let color = addButton.backgroundColor
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = color?.cgColor
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
if presentationStyle == .expanded {
return true
} else {
requestPresentationStyle(.expanded)
return false
}
}
【问题讨论】:
-
您应该完整地发布日志(缺少说明系统破坏了哪些约束的部分)。另外,将日志发布为文本(即代码)。
-
整个堆栈视图固定在屏幕的顶部和侧面--好的,但这并不能完全限制堆栈视图。底部呢?
-
另外,你为什么要发布输出的图像?您无法使用 Mac 的复制/粘贴功能吗?另一方面,如果能看到您的故事板目录的图像,那就太好了。
-
堆栈溢出不会让我粘贴,因为它将标签解释为 HTML 代码(我认为)。
-
堆栈视图有 44pt 高度限制
标签: ios swift autolayout uisearchbar nslayoutconstraint