在安全区域下方显示白框

时间:2017-12-31 03:43:55

标签: ios iphone-x

如何在iPhone X安全区域的底部边缘下方显示一个白框(以便主页指示符位于所述白框上)?我更喜欢以编程方式执行此操作,因此我不会弄乱故事板。

1 个答案:

答案 0 :(得分: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;
}