您好我在UIView上添加了搜索栏。当我运行我的代码时,我可以看到我的搜索栏,但是当我点击搜索栏内部时它隐藏起来,当我再次点击屏幕上的某个地方时,它是可见的。我现在没有得到这个问题。请帮忙。
var searchView:UIView = {
var search = UIView()
search.translatesAutoresizingMaskIntoConstraints = false
search.backgroundColor = UIColor.gray
return search
}()
lazy var searchController : UISearchController = {
var searchController = UISearchController(searchResultsController: nil)
//searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.barTintColor = UIColor.gray
searchController.searchBar.layer.borderWidth = 1
searchController.searchBar.layer.borderColor = UIColor.gray.cgColor
//searchController.dimsBackgroundDuringPresentation = false
searchController.definesPresentationContext = true
searchController.searchBar.sizeToFit()
searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
return searchController
}()
func setUpView(){
view.addSubview(searchView)
searchView.addSubview(searchController.searchBar)
searchView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
searchView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
searchView.topAnchor.constraint(equalTo: view.topAnchor,constant:64).isActive = true
searchView.heightAnchor.constraint(equalToConstant: 65).isActive = true
searchController.searchBar.leftAnchor.constraint(equalTo: searchView.leftAnchor).isActive = true
searchController.searchBar.rightAnchor.constraint(equalTo: searchView.rightAnchor).isActive = true
searchController.searchBar.topAnchor.constraint(equalTo: searchView.topAnchor,constant:10).isActive = true
searchController.searchBar.widthAnchor.constraint(equalTo: searchView.widthAnchor).isActive = true
}
此外,我在 ViewDidLoad() - 中提供了这一行 - :
self.extendedLayoutIncludesOpaqueBars = true
答案 0 :(得分:0)
我发现UISearchController的searchBar在设置translatesAutoresizingMaskIntoConstraints = false时效果不佳。作为一种解决方法,我将搜索栏嵌入到具有所需约束的占位符视图中:
new Date(new Date().toUTCString().substr(0, 25));
注意,searchController.searchBar的translatesAutoresizingMaskIntoConstraints属性保持为真。