【发布时间】:2016-12-18 12:21:50
【问题描述】:
在我的 iOS 应用程序中,我有一个导航栏,由 UIStackView 中的 UISearchBar 和 UIButton 组成。由于我无法为UISearchController 编写故事板,因此我在堆栈视图中有一个空白UIView,我将UISearchBar 添加为子视图。这是我的故事板的样子:
这是我添加搜索栏的代码
override func viewDidLoad() {
super.viewDidLoad()
configureSearchController()
print(wrapperView.bounds)
print(searchController.searchBar.bounds)
}
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
wrapperView.addSubview(searchController.searchBar)
let color = addButton.backgroundColor
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = color?.cgColor
searchController.searchBar.trailingAnchor.constraint(equalTo: addButton.leadingAnchor)
}
【问题讨论】:
-
尝试将按钮移动到uiobject列表中包装视图上方
-
@SaintThread 将
UIButton放在搜索栏前面。 -
对不起,我误解了这个问题,你能展示使用包装视图的约束吗?也许你有父视图的尾随而不是按钮
-
奇怪的是:如果我点击搜索栏并调出键盘,一切正常。我假设那是因为它正在更新视图。有没有办法强制视图更新?这可能会解决我的问题
-
@SaintThread 我对包装视图没有限制,因为它位于堆栈视图中。堆栈视图中不需要约束
标签: ios swift uiview autolayout uisearchbar