MENU
コンテンツ再構築中

Swift: 高さ一覧

アプリ開発中にはオブジェクトの様々なサイズ取得は頻繁に行うことと思います。
いままで bounds や frame のどっちが何を意味するかを深く考えずに、Xcode のサジェストに頼りきりで、知識として定着していませんでした。

今回は、その反省と頭の中を整理するために、頻繁に利用するオブジェクトの「高さ」に焦点を当て、Swift で様々な高さを取得するための一覧をメモしておきます。

INDEX

目次

  • 高さ一覧
  • まとめ

高さ一覧

ステータスバーの高さ

[code]
let statusbarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
[/code]

画面の高さ(ステータスバー含まず)

[code]
let appFrameHeight = UIScreen.mainScreen().applicationFrame.size.height
[/code]

画面の高さ(ステータスバー含む)

[code]
let mainScreenHeight = UIScreen.mainScreen().bounds.size.height
[/code]

ナビゲーションバーの高さ

[code]
let navigationController: UINavigationController = UINavigationController()
let navigationBarHeight = navigationController.navigationBar.frame.size.height
[/code]

ツールバーの高さ

[code]
let toolbarHeight = navigationController.toolbar.frame.size.height
[/code]

タブバーの高さ

[code]
let tabBarController: UITabBarController = UITabBarController()
let tabBarHieght = tabBarController.tabBar.frame.size.height
[/code]

まとめ

いつも UIScreen.mainScreen().applicationFrameUIScreen.mainScreen().bounds でどういった違いがあったか忘れてしまいます。
こうして一覧にしてみてみると applicationFrame は名前の通り「アプリケーションの枠」なので、ステータスバーが含まれないことは自明の理でしたね(笑)

この記事がみなさんのお役に立ちましたら、下記「Share it」よりブックマークやSNSで共有していただければ幸いです。

Please share it!
  • URLをコピーしました!
  • URLをコピーしました!
INDEX