Programmatically get height of navigation bar

前端 未结 14 1125
猫巷女王i
猫巷女王i 2020-12-04 08:02

I know that the presence of the more view controller (navigation bar) pushes down the UIView by its height. I also know that this height = 44px. I have also discovered tha

14条回答
  •  一整个雨季
    2020-12-04 08:43

    Support iOS 13 and Below:

    extension UIViewController {
    
        var topbarHeight: CGFloat {
            if #available(iOS 13.0, *) {
                return (view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0.0) +
                    (self.navigationController?.navigationBar.frame.height ?? 0.0)
            } else {
                let topBarHeight = UIApplication.shared.statusBarFrame.size.height +
                (self.navigationController?.navigationBar.frame.height ?? 0.0)
                return topBarHeight
            }
        }
    }
    

提交回复
热议问题