【发布时间】:2017-12-31 03:43:55
【问题描述】:
如何在 iPhone X 安全区域的底部边缘下方显示一个白框(使主页指示器位于所述白框上)?我更喜欢以编程方式执行此操作,这样我就不会弄乱情节提要了。
【问题讨论】:
-
你想要安全区域的背景颜色是白色的吗?
如何在 iPhone X 安全区域的底部边缘下方显示一个白框(使主页指示器位于所述白框上)?我更喜欢以编程方式执行此操作,这样我就不会弄乱情节提要了。
【问题讨论】:
试试这个(在 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;
}
【讨论】: