【问题标题】:Display a White Box Below Safe Area在安全区域下方显示一个白框
【发布时间】:2017-12-31 03:43:55
【问题描述】:

如何在 iPhone X 安全区域的底部边缘下方显示一个白框(使主页指示器位于所述白框上)?我更喜欢以编程方式执行此操作,这样我就不会弄乱情节提要了。

【问题讨论】:

  • 你想要安全区域的背景颜色是白色的吗?

标签: ios iphone-x


【解决方案1】:

试试这个(在 viewDidLoad 中):

UIView *testView = [UIView new];
testView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:testView];
testView.translatesAutoresizingMaskIntoConstraints = NO;
[testView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor].active = YES;
[testView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
[testView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[testView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;

编辑

如果您只想为 iOS 11+ 完成此操作(这是有道理的)但支持以前的版本,那么请执行以下操作(假设 Xcode 9+):

if (@available(iOS 11.0, *)) {
    UIView *testView = [UIView new];
    testView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:testView];
    testView.translatesAutoresizingMaskIntoConstraints = NO;
    [testView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor].active = YES;
    [testView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
    [testView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
    [testView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
}

【讨论】:

  • 有效!最后一个问题:我得到 'safeAreaLayoutGuide' 仅在 iOS 11.0 或更高版本上可用作为警告。这会对 iOS 9/10 上的设备造成任何问题,还是他们会忽略代码(这很好,因为 iPhone X 是 11+)
  • 确实 safeAreaLayoutGuide 仅在 iOS 11 上可用。如果您希望您的应用程序适用于 iOS 9 和 10,那么您将不得不检测 iOS 版本并做一些不同的事情。事实上,这仅适用于 iPhone X,如果不是 iOS 11,您可以完全跳过它。我将编辑答案以展示如何执行此操作。
  • 谢谢!以及 11.0 之后的 *,我想是指包括 11.0 之后的所有版本?
  • 是的,从技术上讲,它是 @available(x,y,z),它是 iOS 版本 x.y.z,而“”表示任何。所以@available(11,) 是 11.anything,@available(11,2,*) 是 11.2.anything。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 2011-09-08
  • 1970-01-01
相关资源
最近更新 更多