43
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UIStatusBar、UINavigationBarの高さを取得する

Posted at

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
    }
}
43
43
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
43
43

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?