使用safeAreaLayoutGuide没有状态栏(Using safeAreaLayoutGuide without status bar)

编程入门 行业动态 更新时间:2024-10-15 20:23:14
使用safeAreaLayoutGuide没有状态栏(Using safeAreaLayoutGuide without status bar)

我以编程方式添加了所有视图以及它们的自动布局约束。 在我的一个视图中,我使用以下方法禁用了状态栏:

override var prefersStatusBarHidden: Bool { return true }

显然,这会导致safeAreaLayoutGuide在任何不是iPhone X的东西上移动20px。在我的情况下,这导致iPhone 6/7/8 / SE等顶部没有足够的填充。

解决这个问题的最简洁方法是什么? 如何在iPhone 8视图中添加填充而不将其添加到iPhone X?

I am adding all of my views programmatically as well as their auto layout constraints. In one of my views I have disabled the status bar using this:

override var prefersStatusBarHidden: Bool { return true }

Apparently this causes the safeAreaLayoutGuide to be moved by 20px on anything that isn't the iPhone X. And in my case this results in not having enough padding at top for the iPhone 6/7/8/SE etc.

What's the cleanest way to work around this? How would you add padding e.g. on the iPhone 8 views without adding it to the iPhone X?

最满意答案

您可以使状态栏透明以保留空间,而不是使用prefersStatusBarHidden 。 在间距方面,这对iPhone X没有影响。

let statusBarWindow = application.value(forKey: "statusBarWindow") as? UIWindow statusBarWindow.alpha = 0.0

"statusBarWindow"实际上是私有API,因此最好像"status" + "BarWindow"类的那样将它拉开。

You could make the status bar transparent to keep the space instead of using prefersStatusBarHidden. In terms of spacing this will have no effect on iPhone X.

let statusBarWindow = application.value(forKey: "statusBarWindow") as? UIWindow statusBarWindow.alpha = 0.0

"statusBarWindow" is actually private API, so better pull it apart like "status" + "BarWindow" or something like that.

更多推荐

本文发布于:2023-08-06 13:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1449375.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态栏   safeAreaLayoutGuide   bar   status

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!