ios - 从 iOS "safe area"中排除导航栏

标签 ios autolayout ios-autolayout iphone-x

在我的 iOS 应用程序中,我在导航栏下方呈现了 View 。这是因为在用户点击屏幕之前,导航栏是隐藏的。

下面的屏幕截图说明了我的问题。

“X”按钮呈现在 iPhone X 的刘海下方,几乎看不见。这是我将按钮的 topAnchor 限制到它的 superview!.topAnchor 的时候。

Note that this works as intended for all devices except the iPhone X.

enter image description here

此屏幕截图中的“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."

enter image description here

但是,我希望“X”按钮呈现在 iPhone X 的槽口下方和导航栏下方。这是隐藏导航栏时的样子:

enter image description here

“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/

相关文章:

ios - 在没有 super View 的情况下向 .xib 组件添加等宽乘数?

ios - resizableImageWithCapInsets : issues

ios - 在 Xcode 7 中使用 AutoLayout 将 5 个 subview 均匀地分布在其父 View 的宽度上

ios - 嵌入式 dylibs/frameworks 仅支持 iOS 8.0 及更高版本的架构 armv7

ios - 通过长按为不受支持的设备启用 3D 触摸操作

ios - 无法使用 AutoLayout 设置 UICollectionViewCell 内容的宽度

ios - 适用于 iPad 的自适应布局

iphone - 在 UITextView 上使用 NSLayoutConstraint 会将 contentSize 重置为 {0,0}

ios - CAEmitterLayer 负 beginTime 可能吗?

ios - 使垂直空间约束常量等于屏幕高度的百分比