当我在ViewController中单击我的SearchBar时,它将加载一个tableviewcontroller以在视图控制器顶部显示搜索结果。 以前它是行得通的(尽管有很多视觉故障搜索结果从一半开始开始,以此类推,所以我不得不调整一下以为可以一次全部解决)
我查看了无数SO帖子并尝试了以下操作:
VC和TVC的viewDidLoad中的隐藏的导航栏
searchBar开始编辑时隐藏的导航栏
试图在tableViewController的searchBar中添加约束-当前没有约束,因为它们都是在VC中设置的(甚至不确定那是个好主意)
tried resultSearchController?.automaticallyAdjustsScrollViewInsets = true
tried resultSearchController?.hidesNavigationBarDuringPresentation = true
tried definesPresentationContext = true
tried self.navigationController?.extendedLayoutIncludesOpaqueBars = true
tried changing position for better view + slowing animations
tried self.resultSearchController?.isActive = true
self.tableView.tableHeaderView =
self.resultSearchController?.searchBar
searchBar.sizeToFit()
更多
但是我想知道是否这些都不会起作用,因为它已经在某种程度上起作用了。 不同之处可能是我认为我已经以编程方式创建了视图,并且它们也在子视图上创建(它们在卡片上)。层次结构是:
查看/ CardView / SearchBar1 + SearchBar 2
另一件事是,即时通讯没有在VC中使用导航栏来显示searchBar,而且我没有任何代码可在TVC中指定位置-所以我认为这就是问题所在。 (我认为这是由searchController处理的)
我认为也很重要:单击搜索栏时,我可以非常简短地看到搜索栏跳到屏幕的左上角,然后慢慢向左滑动。它只是一瞬间。但这可能与我呈现tableViewController的方式有关?
如您所见,即时通讯卡住了。我的猜测与视图和自动布局有关,但是如果有人可以将我引向一个普遍的方向,那么我可能会非常感激这个问题。
作为旁注,如果有任何用处,我可以在演示的VC中包含以下代码:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: false)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
}
在VC中:
func configureResultSearchController() {
let userSearchTableVC = storyboard!.instantiateViewController(withIdentifier: "UserSearchTableVC") as! UserSearchTableVC
resultSearchController = UISearchController(searchResultsController: userSearchTableVC)
resultSearchController?.searchResultsUpdater = userSearchTableVC
resultSearchController?.dimsBackgroundDuringPresentation = false
resultSearchController?.view.backgroundColor = .white
resultSearchController?.hidesNavigationBarDuringPresentation = false
userSearchTableVC.resultSearchController = resultSearchController
definesPresentationContext = true
}
// Hierarchy -> View / Card View / SearchBar
var searchBarTopAnchor: NSLayoutConstraint?
func addSearchBarToCard() {
cardView.addSubview(searchBar)
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.leadingAnchor.constraint(equalTo: cardView.leadingAnchor, constant: 0).isActive = true // constant is 0 by default, could be any else
searchBar.trailingAnchor.constraint(equalTo: cardView.trailingAnchor, constant: 0).isActive = true // constant is 0 by default, could be any else
searchBarTopAnchor = searchBar.topAnchor.constraint(equalTo: cardView.topAnchor, constant: 0)
searchBarTopAnchor?.isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
在UserSearchTableVC中,除了更改searchBar的颜色等之外,没有其他任何与searchBar的布局有关的内容。(因为它事先可见,而且仍在起作用-即我可以搜索并且结果正在出现,它只是搜索栏的位置)。但是我确实尝试过,但也没有用:
override func viewWillAppear(_ animated: Bool) {
guard let searchBar = resultSearchController?.searchBar else {return}
view.addSubview(searchBar)
searchBar.translatesAutoresizingMaskIntoConstraints = false
searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true // constant is 0 by default, could be any else
searchBar.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true // constant is 0 by default, could be any else
searchBar.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
searchBar.layoutIfNeeded()
searchBar.sizeToFit()
searchBar.layoutIfNeeded()
searchBar.layer.shadowColor = nil
searchBar.layer.shadowOpacity = 0
searchBar.layer.shadowOffset = .zero
searchBar.layer.shadowRadius = 0
searchBar.layer.shadowOffset = .zero
}