在安全区域下方显示一个白框

Display a White Box Below Safe Area

如何在 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;
}