Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
44
Help us understand the problem. What is going on with this article?
@to4iki

UIStatusBar、UINavigationBarの高さを取得する

More than 5 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
    }
}
44
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
44
Help us understand the problem. What is going on with this article?