iOS safeAreaLayoutGuide 为 iPhone X 提供完整的 812 高度
iOS safeAreaLayoutGuide gives full 812 height for iPhone X
这在 Why does this iPhone X UIView not get safeAreaLayoutGuide?
中也有描述但没有回答
我创建了一个全新的从头开始的单视图通用 iOS 应用程序,并将 仅 这三行代码添加到 viewDidLoad()
。我以纵向模式启动 iPhone X 模拟器。这是我得到的:
let safeSize = view.safeAreaLayoutGuide.layoutFrame.size
// Returns (375.0, 812.0) — why full size?
let window = UIApplication.shared.delegate!.window!!
let safeSize2 = window.safeAreaLayoutGuide.layoutFrame.size
// Returns (375.0, 734.0) — this is what I expected.
为什么根视图控制器的根视图不是return预期的安全区域?
SafeAreaInsets:
You obtain the safe area for a view by applying the insets in this
property to the view's bounds rectangle. If the view is not currently
installed in a view hierarchy, or is not yet visible onscreen, the
edge insets in this property are 0.
SafeAreaLayoutGuide也是如此:
When the view is visible onscreen, this guide reflects the portion of
the view that is not covered by navigation bars, tab bars, toolbars,
and other ancestor views.
因此,由于视图尚未在 viewDidLoad
中布局或可见,因此它为 0.. 当您在 viewDidLayoutSubviews
中进行布局时,您将看到它是非零且有效的插图已设置。
这在 Why does this iPhone X UIView not get safeAreaLayoutGuide?
中也有描述但没有回答我创建了一个全新的从头开始的单视图通用 iOS 应用程序,并将 仅 这三行代码添加到 viewDidLoad()
。我以纵向模式启动 iPhone X 模拟器。这是我得到的:
let safeSize = view.safeAreaLayoutGuide.layoutFrame.size
// Returns (375.0, 812.0) — why full size?
let window = UIApplication.shared.delegate!.window!!
let safeSize2 = window.safeAreaLayoutGuide.layoutFrame.size
// Returns (375.0, 734.0) — this is what I expected.
为什么根视图控制器的根视图不是return预期的安全区域?
SafeAreaInsets:
You obtain the safe area for a view by applying the insets in this property to the view's bounds rectangle. If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the edge insets in this property are 0.
SafeAreaLayoutGuide也是如此:
When the view is visible onscreen, this guide reflects the portion of the view that is not covered by navigation bars, tab bars, toolbars, and other ancestor views.
因此,由于视图尚未在 viewDidLoad
中布局或可见,因此它为 0.. 当您在 viewDidLayoutSubviews
中进行布局时,您将看到它是非零且有效的插图已设置。