6

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

Actually I have used this code on viewdidload method:

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

Please help me fix this.

Krunal
  • 77,632
  • 48
  • 245
  • 261
sritharan
  • 135
  • 1
  • 1
  • 10
  • 1
    The problem here is the nil unwrapping exception, which is most likely caused by the statement `(self.navigationController?.navigationBar.frame.size.height)!`. It suggests that some part of that expression is not initialised — are you sure that your view controller is inside a navigation controller? What happens if you put a breakpoint there and check the value of `self.navigationController`, by typing `po self.navigationController` into the debug console? – Craig McMahon Feb 25 '16 at 10:20

4 Answers4

9

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

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

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)

For ease, try this UIViewController extension

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)
Krunal
  • 77,632
  • 48
  • 245
  • 261
2

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

self.navigationController?.navigationBar.intrinsicContentSize.height
Ram Madhavan
  • 2,362
  • 1
  • 17
  • 20
0
let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
  • 3
    I would do it slightly differently: `let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0` – Yuval Tal Feb 10 '17 at 20:56
0

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

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")
    }

}
Pradeep K
  • 3,671
  • 1
  • 11
  • 15