xcode 如何以编程方式在 iOS 中获得导航栏高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35624017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:35:04  来源:igfitidea点击:

How to get a navigation bar hight in iOS programatically

iosobjective-cswiftxcodeuinavigationbar

提问by sritharan

I have to move up my view when keyboardwillshownthen it back to it's normal place when keyboard will hide

keyboardwillshown当键盘将隐藏时,我必须向上移动我的视图然后它回到它的正常位置

Actually I have used this code on viewdidloadmethod:

实际上我在viewdidload方法上使用了这个代码:

override func viewDidLoad() { 

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil) 

}

Here is the method which I used for move up and down

这是我用来上下移动的方法

func keyboardWillShow(notification:NSNotification){ 

    print("keyboardwillshow")
    let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()

    self.view.frame = CGRectMake(0.0, -keyboardSize!.size.height + (self.navigationController?.navigationBar.frame.size.height)! + 20 , self.view.frame.size.width, self.view.frame.size.height )  

}

func keyboardWillHide(notification:NSNotification){

    self.view.frame = CGRectMake(0.0,(self.navigationController?.navigationBar.frame.size.height)!, self.view.frame.size.width, self.view.frame.size.height)
//  self.view.frame.origin.y += 350

}

While I run this program, it will show a run time error that is

当我运行这个程序时,它会显示一个运行时错误

unexpected found a nil while unwrapping

在解包时意外发现一个 nil

Please help me fix this.

请帮我解决这个问题。

回答by Krunal

With iPhone-X, height of top bar (navigation bar + status bar) is changed (increased).

使用 iPhone-X,顶部栏(导航栏 + 状态栏)的高度发生了变化(增加)。

Try this if you want exact height of top bar (both navigation bar + status bar):

如果您想要顶部栏的确切高度(导航栏 + 状态栏),请尝试此操作:

Objective-C

目标-C

CGFloat topbarHeight = ([UIApplication sharedApplication].statusBarFrame.size.height +
       (self.navigationController.navigationBar.frame.size.height ?: 0.0));

Swift 4

斯威夫特 4

let topBarHeight = UIApplication.shared.statusBarFrame.size.height +
        (self.navigationController?.navigationBar.frame.height ?? 0.0)

For ease, try this UIViewController extension

为方便起见,试试这个 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

斯威夫特 3

let topBarHeight = UIApplication.sharedApplication().statusBarFrame.size.height +
(self.navigationController?.navigationBar.frame.height ?? 0.0)

回答by Ram Madhavan

In Swift 3You will get the navigation bar height by using the following way:

Swift 3 中,您将使用以下方式获取导航栏高度:

self.navigationController?.navigationBar.intrinsicContentSize.height

回答by Sourabh Sharma

let navigationBarHeight: CGFloat = self.navigationController!.navigationBar.frame.height

回答by Pradeep K

You should protect your code against nil values this way. it will also help in using your view controllers inside navigation controllers and outside.

您应该通过这种方式保护您的代码免受 nil 值的影响。它还有助于在导航控制器内部和外部使用您的视图控制器。

func keyboardWillShow(notification:NSNotification){

    print("keyboardwillshow")
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {
        if let navBarHeight = self.navigationController?.navigationBar.frame.size.height {
            self.view.frame = CGRectMake(0.0, -keyboardSize.size.height + navBarHeight + 20 , self.view.frame.size.width, self.view.frame.size.height )
        } else {
            print("could not get navbar height")
            self.view.frame = CGRectMake(0.0, -keyboardSize.size.height + 20 , self.view.frame.size.width, self.view.frame.size.height )
        }
    } else {
        print("no keyboard size property")
    }

}