ホームボタンの有無で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画面サイズ早見表(図付き)