我有一个导航栏和一个集合视图以编程方式添加到我的应用程序中,我也在尝试添加一个自定义视图。我已经让视图位于集合视图的上方和导航栏的下方,这正是我想要的,然而,在iOS 11上,导航栏的高度会根据您是否向下拉伸集合视图而变化。我希望视图也向下移动,以便在集合视图和导航栏之间没有空白,因为我的自定义视图不会向下移动,因为视图的安全插入不会更改。它将向上走向崩溃的标题,这不是一个问题。
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Home"
navigationController?.navigationBar.prefersLargeTitles = true
collectionView?.backgroundColor = UIColor.white
collectionView?.translatesAutoresizingMaskIntoConstraints = false
//collectionView?.contentInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
//collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
collectionView?.register(StockCell.self, forCellWithReuseIdentifier: "cellid")
setupMenuBar()
}
let menuBar: MenuBar = {
let mb = MenuBar()
mb.translatesAutoresizingMaskIntoConstraints = false
mb.backgroundColor = .black
return mb
}()
private func setupMenuBar() {
view.addSubview(menuBar)
NSLayoutConstraint.activate([
(collectionView?.topAnchor.constraint(equalTo: menuBar.bottomAnchor))!,
(collectionView?.widthAnchor.constraint(equalTo: view.widthAnchor))!,
(collectionView?.bottomAnchor.constraint(equalTo: view.bottomAnchor))!,
menuBar.heightAnchor.constraint(equalToConstant: 50),
menuBar.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
menuBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
}
This is the starting screen, showing the view in its resting position
This is the navigation bar overlapping the view, which is what I want to fix
最佳答案
您可以将自定义菜单视图的底部锚定限制为导航栏的底部锚定;这样,菜单视图将始终保持相同的垂直偏移(但您可能需要适应其他一些限制):
navigationController!.navigationBar.bottomAnchor.constraint(equalTo: menuBar.bottomAnchor)
有关扩展示例,请参见this answer。
关于ios - 如何将UIView固定在iOS 11 NavigationBar不断变化的高度框架下?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48718086/