简体   繁体   English

如何在 Swift 的 UIView/Utility 类中获取导航栏

[英]How do I get Navigation Bar in UIView/Utility class in Swift

Actually I need to get the navigation bar in custom view class.实际上我需要在自定义视图类中获取导航栏。 I can get the navigaiton bar height in my view controller class.我可以在我的视图控制器类中获得导航栏高度。 How do I get the naviagtion bar height in the UIView/Utiity common class?如何在 UIView/Utiity 通用类中获取导航栏高度? . . I have only one view controller and added navigation controller in that class.我只有一个视图控制器并在该类中添加了导航控制器。 I want to access that navigation bar height in any of the custom view.我想在任何自定义视图中访问该导航栏高度。

    // works in view controller 
    var navigationBarHeight: CGFloat {
    return UIApplication.shared.statusBarFrame.size.height +
        (self.navigationController?.navigationBar.frame.height ?? 0.0)
}

You can get the height of navigation bar by using following:您可以使用以下方法获取导航栏的高度:

guard let rootVC = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController else { return }

let navigationBarHeight = rootVC.navigationBar.frame.height

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM