2

Я пытаюсь получить размер клавиатуры от userInfo с использованием расширения клавиатуры.Уведомление о клавиатуре

добавить

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardDidShowNotification, object: nil) 

внутри моей KeyboardViewController.swift но keyboardWillShow никогда не вызывается.

ответ

0

В видуWillAppear, вы добавили self.subscribeToKeyboardNotifications()? Догадаться, ваш KeyboardViewController отдельно от ViewController, в котором появляется клавиатура? Если это так, используйте `NSNotificationCenter.defaultCenter(). AddObserver (self, selector:" keyboardWillShow: ", name: UIKeyboardDidShowNotification, object: nil) в func в реальном ViewController.

7

Первый регистр для клавиатуры уведомлений

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    let notificationCenter = NSNotificationCenter.defaultCenter() 
    notificationCenter.addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil) 
    notificationCenter.addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil) 
} 

override func viewDidDisappear(animated: Bool) { 
    super.viewDidDisappear(animated) 

    let notificationCenter = NSNotificationCenter.defaultCenter() 
    notificationCenter.removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) 
    notificationCenter.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) 
} 

затем

func keyboardWillShow(notification: NSNotification) { 
    let userInfo = notification.userInfo! 
    let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height 
} 

и у вас есть значение высоты как CGFloat

0

используйте следующий код для Swift 3.0

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 

    let notificationCenter = NotificationCenter.default 
    notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    notificationCenter.addObserver(self, selector: #selector(SignUpViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

override func viewDidDisappear(_ animated: Bool) { 
    super.viewDidDisappear(animated) 

    let notificationCenter = NotificationCenter.default 
    notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
} 

@objc func keyboardWillShow(notification: NSNotification) { 
    let userInfo = notification.userInfo! 
    let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.height 

    UIView.animate(withDuration: 0.3) { 

     self.constraintBottomBtnNext.constant = keyboardHeight; 
    } 
    self.view.layoutIfNeeded(); 

    print(keyboardHeight); 
} 

@objc func keyboardWillHide(notification: NSNotification) { 

    UIView.animate(withDuration: 0.3) { 

     self.constraintBottomBtnNext.constant = 0; 
    } 

    self.view.layoutIfNeeded(); 
}