简体   繁体   English

导航栏正下方的 Position 元素 (SwiftUI)

[英]Position Element Directly Below Navigation Bar (SwiftUI)

I am trying to figure out how to position a Rectangle() just below the navigation bar (there should be no space between these two elements).我试图弄清楚如何在导航栏下方 position 一个 Rectangle() (这两个元素之间应该没有空格)。 So far the only solution I have found is to guess the height of the navigation bar and offset the Rectangle() by that amount.到目前为止,我发现的唯一解决方案是猜测导航栏的高度并将 Rectangle() 偏移该数量。 Is there any way to get the height of the navigation bar programmatically or to pin an element directly under the navigation bar.有没有办法以编程方式获取导航栏的高度或将元素直接固定在导航栏下方。 I need a solution which allows me to adapt the position of my Rectangle() based on screen size.我需要一个解决方案,它允许我根据屏幕尺寸调整我的 Rectangle() 的 position。 Thanks in advance.提前致谢。

Assuming your parent view has no restrictions, use this code假设您的父视图没有限制,请使用此代码

YourRectangle()
   .frame(width: UIScreen.main.bounds.width * 0.5, height: UIScreen.main.bounds.height * 0.5)

This will put your view in the center这将使您的视图居中

Answer: the NavigationBar has the height of the safe area.答:NavigationBar 有安全区域的高度。 Hence, if you have configured your view with the modifier.edgesIgnoringSafeArea(.all) UIElements can render beneath the Navigation Bar.因此,如果您使用修饰符配置视图。edgesIgnoringSafeArea(.all) UIElements 可以在导航栏下方呈现。 My fix came by removing the edgesIgnoringSafeArea modifier from the outermost ZStack which prevented the rectangle from appearing at a y-value less than the safe area height.我的解决方法是从最外层的 ZStack 中删除 edgesIgnoringSafeArea 修饰符,这可以防止矩形出现在小于安全区域高度的 y 值处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM