Posted at

UIStatusBar、UINavigationBarの高さを取得する

More than 3 years have passed since last update.

StatusBarの高さ

let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height

navigationBarの高さ

func navigationBarHeight(callFrom: UIViewController) -> CGFloat? {

return callFrom.navigationController?.navigationBar.frame.size.height
}

Viewに関する情報を集約したManagerクラス/構造体で管理するのが良いかと思います

struct ViewManager {

static let rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController

static let statusBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height

static var currentWindow: UIWindow? {
if let window = UIApplication.sharedApplication().keyWindow {
return window
} else {
return UIApplication.sharedApplication().windows[0] as? UIWindow
}
}

// デフォルトFloat(44)としてUnwrap
static func navigationBarHeight(callFrom: UIViewController) -> CGFloat {
return callFrom.navigationController?.navigationBar.frame.size.height ?? 44
}
}