自动层,安全区域和导航栏的问题
在NavigationBar的存在下,我在使用自动布局方面遇到令人讨厌的问题。我的目标是将视图放置在导航栏和工具栏后面。基本上,我不想将我的子视图限制为导航栏或工具栏。这样,如果我让它们消失,我的观点将留在原处。
但是,无论我做什么,似乎都失败了。
这是我的代码,在ViewController的ViewDidload()中:
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
我的理解是,以这种方式,我可以忽略Safeareas。但是,看来底部的锚是正确的,而Topanchor与视图一致。
视图控制器是导航控制器的根。
有什么想法吗?
I'm having nasty problems with using auto layout in presence of a navigationBar. My goal is to position the view behind the navigation bar and the toolbar. Basically, I don't want to constrain my subview to either the navigation bar or the toolbar. This way, if I make them disappear, my view will stay where it is.
However, no matter what I do, it seems to fail.
Here is my code, inside the viewController's viewDidLoad():
view.addSubview(collectionView)
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
My understanding was that in this way I could ignore the safeareas. However, it seems that the bottom anchor is correct while the topAnchor coincides with the view.safeAreaLayoutGuide.topAnchor.
The view controller is the root of a navigationController.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,无数小时后,我弄清楚了。
如果使用不透明的导航栏,则必须修改ViewController的此属性:
没有它,当可见导航栏时,View的Topanchor对应于导航栏的底部(与视图的框架相同)。
So, after countless hours I figured this out.
If you are using an opaque navigation bar, you have to modify this property of the ViewController:
Without it, when the navigation bar is visible, the view's topAnchor corresponds to the bottom of the navigation bar (same thing with the view's frame).