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

标签 ios iphone-x

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

最佳答案

试试这个(在 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;
}

关于ios - 在安全区域下方显示一个白框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039792/

相关文章:

ios - 如何在 iPhone X 上滚动时隐藏状态栏上的内容

ios - 为 iPhone X 处理自定义标签栏

ios - react native iOS : Could not build module 'yoga' : 'algorithm' file not found

ios - Swift 3 - 如何处理 URLRequest/URLSession 错误?

ios - Swift:iPhone 上的安全区域

ios - inputAccessoryView 在 iPhone X 上未获取触摸事件

ios - 在 iPhone X 上使 WKWebview "real"全屏(从 WKWebView 中删除安全区域

ios - WKWebView:添加加载URL的按钮

iphone - 在单个 UIViewController 中有多个 UIView 的障碍

ios - 如何在 ios 中与 ms word 共享文件夹?