
細かいレイアウトの調整をしていると、statusbarやsafeAreaに悩ませれることがあります。
これらで高さの取得ができます。
1 2 3 4 5 6 7 8 |
// // statusbarの高さの取得(height/width) let statusBarHeight = UIApplication.shared.statusBarFrame.size.height // safeAreaの高さを取得(bottom/left/right/top) let safeAreaBottom = self.view.safeAreaInsets.bottom |
また取得するタイミングですが、
viewDidLayoutSubviewsメソッド内で取得するのが良いようです。ただ、一番早く取れるのはviewWillLayoutSubviewsのようです。参考
そのメソッドの中でうまく取得できなければview.layoutIfNeeded()が必要な場合もあるようです。