Programmatically get height of navigation bar

前端 未结 14 1147
猫巷女王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:52

    iOS 14

    For me, view.window is null on iOS 14.

    extension UIViewController {
        var topBarHeight: CGFloat {
            var top = self.navigationController?.navigationBar.frame.height ?? 0.0
            if #available(iOS 13.0, *) {
                top += UIApplication.shared.windows.first?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
            } else {
                top += UIApplication.shared.statusBarFrame.height
            }
            return top
        }
    }
    

提交回复
热议问题