在我的 iOS 应用程序中,我在导航栏下方呈现了 View 。这是因为在用户点击屏幕之前,导航栏是隐藏的。
下面的屏幕截图说明了我的问题。
“X”按钮呈现在 iPhone X 的刘海下方,几乎看不见。这是我将按钮的 topAnchor
限制到它的 superview!.topAnchor
的时候。
Note that this works as intended for all devices except the iPhone X.
此屏幕截图中的“X”按钮锚定到其 superview!.safeAreaLayoutGuide.topAnchor
并呈现在导航栏下方。考虑到 Apple 关于 safeAreaLayoutGuide
的文档,这是有道理的:
"this guide reflects the portion of the view that is not covered by navigation bars, tab bars, toolbars, and other ancestor views."
但是,我希望“X”按钮呈现在 iPhone X 的槽口下方和导航栏下方。这是隐藏导航栏时的样子:
“X”按钮应该呈现在槽口的正下方。
所以我的问题是:
有没有办法从 View 的 safeAreaLayoutGuide
中排除导航栏?如果没有,除了手动偏移 iPhone X 上的按钮外,我还有什么选择。
Note that I'm doing everything programmatically. I no longer use Storyboards.
谢谢!
最佳答案
您可以通过添加额外的插图来更改 View Controller 安全区域插图。
创建一个 UIEdgeInsetsMake()
对象,在其中从顶部插图中减去导航栏高度。然后将其添加到 View Controller additionalSafeAreaInsets
。
声明:var additionalSafeAreaInsets: UIEdgeInsets { get set }
关于ios - 从 iOS "safe area"中排除导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50364565/