LoginSignup
4
7

More than 3 years have passed since last update.

【swift】 iPhone 8 以前と iPhone X 以降で UIToolBar の高さを変える。

Last updated at Posted at 2019-06-20

ToolBar.gif

ホームボタンの有無でUIToolBarの高さを変えたい。

iPhone 8 以前のホームボタンがあるモデルと、iPhone X 以降のホームボタンがないモデルで UIToolBar の高さを変える方法を示します。
iPhone X 以降では画面下端に細いバーが入るので、若干 UIToolBarを高くして、BarButtonItemが細いバーにかぶらないようにします。
(Auto Layout は使わない設定です。)

コード

UIToolBar を "myToolBar" とします。

import UIKit

class ViewController: UIViewController {

    @IBOutlet var myToolBar:UIToolbar!

    override func viewDidLoad() {
        super.viewDidLoad()
        let width = UIScreen.main.bounds.size.width
        let height = UIScreen.main.bounds.size.height
        //iPhone X 以降で、以下のコードが実行されます
        if height > 800.0 && height < 1000.0{
            myToolBar.frame = CGRect(x: 0, y: height * 0.92, width: width, height: height * 0.055)
        }
    }
}

「y: height * 0.92」や「height: height * 0.055」の数字は好みで微調整して下さい。

参考サイト
iPhone画面サイズ早見表(図付き)

4
7
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
4
7