2017-12-31 44 views
0

iPhoneのセーフエリアの下端の下に白いボックスを表示するにはどうすればいいですか?私はプログラムでこれをやりたいので、私はストーリーボードを乱さないでください。セーフエリアの下に白いボックスを表示

+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; 

EDIT

あなたはこれだけ(理にかなって)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; 
} 
+0

作品!最後の質問:「safeAreaLayoutGuide」は、iOS 11.0以降でのみ警告として利用できます。これにより、iOS 9/10のデバイスに問題が起きるか、iPhoneのXが11+以降のコードは無視されます。 – Branch

+1

実際、safeAreaLayoutGuideはiOS 11でのみ使用できます。アプリをiOS 9で動作させたい場合&10なら、あなたはiOSのバージョンを検出して別の何かをしなければならないでしょう。実際、これはiPhone Xのためのものなので、iOS 11でなければ、これを完全にスキップすることができます。これを行う方法を示す答えを編集します。 –

+0

ありがとう! * 11.0以降の*は、11.0以降のすべてのバージョンを意味します。 – Branch

関連する問題