LoginSignup
0
4

【Swift】ホームボタンのあるiPhoneと無いiPhoneでレイアウトの処理を分ける

Last updated at Posted at 2021-04-19

Swiftのレイアウトで厄介なのが

「iPhone6やiPhone8などのホームボタンがあるiPhone」

「iPhoneXSやiPhone11などのホームボタンが無いiPhone」

とでレイアウトがずれてしまう問題。

このホームボタンのあるiPhoneと無いiPhoneでレイアウトの処理を分けるコードを書いたのでメモとして残しておきます。

    if UIScreen.main.nativeBounds.height > 2208 || UIScreen.main.nativeBounds.height == 1792{
        //ホームボタンがあるiPhoneでの処理
    }
    else{
        //ホームボタンが無いiPhoneでの処理
    }

要は画面の縦幅から振り分けています。

縦幅の詳細はこちらの記事を参考にしました。

最初は2208をラインに振り分けていたのですが、iPhone11とiPhoneXRだけ縦幅のpxが2208より大幅に少ない1792でしたのでそれも考慮。

*iPadについては考慮していません。

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4