我正在以编程方式执行所有操作,并且没有 Storyboard 。我使用的是 Swift2.0,我是初学者。我的应用程序结构非常简单:
在我的 AppDelegate 中,我正在初始化一个 UINavigationController
和一个 ViewController,并将我的 ViewController 添加到我的 navigationController。
在我的 ViewController 的 loadView()
方法中,我初始化了我的 View :self.view = myView()
现在我在 myView 类中,我正在尝试构建我的 View (制作 4 个按钮,添加约束)。现在,当我想要制作按钮时,我需要知道 navigationController 的 navigationBar 大小(高度)。
如何从我的 View 类访问它?执行此操作的最佳方法是什么?
谢谢。
最佳答案
如果你想要顶栏(导航栏+状态栏)的精确高度,试试这个:
objective-C
CGFloat topbarHeight = ([UIApplication sharedApplication].statusBarFrame.size.height +
(self.navigationController.navigationBar.frame.size.height ?: 0.0));
swift 4
let topBarHeight = UIApplication.shared.statusBarFrame.size.height +
(self.navigationController?.navigationBar.frame.height ?? 0.0)
为方便起见,试试这个 UIViewController 扩展
extension UIViewController {
/**
* Height of status bar + navigation bar (if navigation bar exist)
*/
var topbarHeight: CGFloat {
return UIApplication.shared.statusBarFrame.size.height +
(self.navigationController?.navigationBar.frame.height ?? 0.0)
}
}
swift 3
let topBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height +
(self.navigationController?.navigationBar.frame.height ?? 0.0)
关于ios - 如何从我的 iOS View 访问导航 Controller 导航栏大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610785/