Ios 搜索栏在点击时隐藏
嗨,我在UIView上添加了搜索栏。当我运行我的代码时,我可以看到我的搜索栏,但当我点击搜索栏内部时,它隐藏了自己,当我再次点击屏幕上的某个地方时,它是可见的。我现在没有遇到这个问题。请帮助Ios 搜索栏在点击时隐藏,ios,swift,swift3,uisearchcontroller,Ios,Swift,Swift3,Uisearchcontroller,嗨,我在UIView上添加了搜索栏。当我运行我的代码时,我可以看到我的搜索栏,但当我点击搜索栏内部时,它隐藏了自己,当我再次点击屏幕上的某个地方时,它是可见的。我现在没有遇到这个问题。请帮助 var searchView:UIView = { var search = UIView() search.translatesAutoresizingMaskIntoConstraints = false search.backgroundColor =
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
我发现当UISearchController的搜索栏设置为translatesAutoResizengMaskintoConstraints=false时,它的工作不正常。作为一种解决方法,我将搜索栏嵌入到具有所需约束的占位符视图中:
let searchBarPlaceholderView = UIView()
searchBarPlaceholderView.addSubview(searchController.searchBar)
searchBarPlaceholderView.translatesAutoresizingMaskIntoConstraints = false
searchBarPlaceholderView.heightAnchor.constraint(equalToConstant: 56).isActive = true
stackView.addArrangedSubview(searchBarPlaceholderView)
请注意,searchController.searchBar的translatesAutoresizingMaskIntoConstraints属性保留为true。我不理解这个问题,所以您希望它在点击外部时可见,还是不希望它可见?您的评论:“我现在不明白这个问题。”这让我更加困惑。@brw59我的观点是,当我点击UISearchBar字段内部时,UISearchBar会隐藏自己,我看不到它,但当我再次点击控制器屏幕上的某个位置时,它会再次显示。所以我不想让它隐藏。