我尝试向上移动UITextField,但如果使用导航控制器则不起作用,print(self.view.frame.origin.y)
写-216
如果您不使用导航控制器
,一切都很完美class RegistratoinViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var confirmPasswordTextField: UITextField!
var activeTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
singUpButton.enabled = false
self.hideKeyboardWhenTappedAround()
self.nameTextField.delegate = self
self.emailTextField.delegate = self
self.passwordTextField.delegate = self
self.confirmPasswordTextField.delegate = self
self.nameTextField.keyboardAppearance = UIKeyboardAppearance.Dark
self.emailTextField.keyboardAppearance = UIKeyboardAppearance.Dark
self.passwordTextField.keyboardAppearance = UIKeyboardAppearance.Dark
self.confirmPasswordTextField.keyboardAppearance = UIKeyboardAppearance.Dark
self.emailTextField.keyboardType = UIKeyboardType.EmailAddress
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(RegistratoinViewController.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(RegistratoinViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: self.view.window)
}
func keyboardWillHide(sender: NSNotification) {
let userInfo: [NSObject : AnyObject] = sender.userInfo!
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
self.view.frame.origin.y += keyboardSize.height
}
override func viewWillDisappear(animated: Bool) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)
}
func keyboardWillShow(sender: NSNotification) {
let userInfo: [NSObject : AnyObject] = sender.userInfo!
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size
if keyboardSize.height == offset.height {
if self.view.frame.origin.y == 0 {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.frame.origin.y -= keyboardSize.height
})
}
} else {
UIView.animateWithDuration(0.1, animations: { () -> Void in
self.view.frame.origin.y += keyboardSize.height - offset.height
})
}
print(self.view.frame.origin.y)
}
}
答案 0 :(得分:0)
如果使用滚动视图并在显示键盘时滚动它,将会更容易。在这里,您可以查看如何执行此操作:
http://blog.apoorvmote.com/move-uitextfield-up-when-keyboard-appears/?lang=ca