自动层,安全区域和导航栏的问题

发布于 2025-02-12 23:15:19 字数 708 浏览 1 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-02-19 23:15:19

因此,无数小时后,我弄清楚了。

如果使用不透明的导航栏,则必须修改ViewController的此属性:

self.extendedLayoutIncludesOpaqueBars = true

没有它,当可见导航栏时,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:

self.extendedLayoutIncludesOpaqueBars = true

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文