我的申请表中有登录和注册表格。当用户点击任何textField时,我会计算键盘和textField的高度差,并在需要时滚动我的视图。
为此,我在loginVC中添加了两个观察者:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
println("keyb show")
keyboardIsShown = true
keyboardHeight = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().height
calculateAnimationValue()
}
func keyboardWillHide(notification: NSNotification) {
println("keyb hide")
keyboardIsShown = false
animateViewMoving(false, moveValue: animationValue)
animationValue = 0
}
func textFieldDidBeginEditing(textField: UITextField) {
pressedTextField = textField
textField.becomeFirstResponder()
if keyboardIsShown {
calculateAnimationValue()
}
}
func calculateAnimationValue() {
if keyboardHeight != nil {
var windowHeight = self.view.frame.height
var textFieldY = pressedTextField.frame.origin.y
var windowWithoutKeyboard = windowHeight - keyboardHeight - 44
if textFieldY > windowWithoutKeyboard {
if animationValue < (textFieldY - windowWithoutKeyboard) {
animationValue = animationValue + textFieldY - windowWithoutKeyboard
animateViewMoving(true, moveValue: animationValue)
}
}
}
println(animationValue)
}
func animateViewMoving (up: Bool, moveValue: CGFloat){
var movementDuration: NSTimeInterval = 0.3
var movement:CGFloat = (up ? -moveValue : moveValue)
UIView.beginAnimations("animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
但是我想在registrationVC中做同样的事情。
如何在另一个VC中使用keyboardWillShow,keyboardWillHide?
我尝试添加相同的观察者但是应用程序在loginVC中不在registrationVC中触发这些方法。
有人可以帮助我吗?
更新:------------------------
class LogInViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
println("keyb show")
}
func keyboardWillHide(notification: NSNotification) {
println("keyb hide")
}
}
class RegistrationViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
println("keyb show2")
}
func keyboardWillHide(notification: NSNotification) {
println("keyb hide2")
}
deinit {
println("deinit")
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
}
如果我在loginVC上触摸textField并且segue到registerVC它运行良好并打印“keyb show2”。
但是如果没有在loginVC上触摸textField并且segue到registerVC并触摸textField则会打印“keyb show”。这是从以前的控制器调用的。我做错了什么?