简体   繁体   English

切换UITextField(Swift)时出现键盘调整视图

[英]Adjust View for keyboard appears when switching UITextField (Swift)

I have two UIView's , the Main one and one which will move up when the keyboard appears (Second UIView has two textfields). 我有两个UIView,主要的一个和一个将在键盘出现时向上移动(第二个UIView有两个文本字段)。 I've managed to do this with the following code: 我已经用下面的代码做到了:

LoginViewController.swift : LoginViewController.swift:

override func viewDidLoad() {
    super.viewDidLoad()


    self.originalCenter = self.contentView.center;
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardDidShowNotification, object: nil)


}

func keyboardDidShow(notification: NSNotification)
{
    UIView.animateWithDuration(1.0, animations: {
    self.contentView.center = CGPointMake(self.originalCenter.x, self.originalCenter.y - 40);
     })

}

My big problems is that because the movement of the UIView depends on the keyboard notification, when the user taps one TextField it moves up as it should, but when he taps the second one the view moves down automatically. 我的大问题是,由于UIView的移动取决于键盘通知,因此当用户单击一个TextField时,它会按原样向上移动,但是当用户单击第二个TextField时,视图会自动向下移动。 What Im I doing wrong? 我在做什么错?

This is what is happening: Keyboard Error Gif 这是正在发生的事情: 键盘错误Gif

This question was answered over here , but I need the solution for Swift language. 在这里回答这个问题,但是我需要Swift语言的解决方案。

Create an instance variable and check whether the keyboard was activated or not: class ViewController: UIViewController, UITextFieldDelegate { 创建一个实例变量,然后检查键盘是否已激活:类ViewController:UIViewController,UITextFieldDelegate {

var _keyboardActivated: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()
    registerForKeyboardNotifications()
}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self,
        name: UIKeyboardDidShowNotification,
        object: nil)

    NSNotificationCenter.defaultCenter().removeObserver(self,
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func registerForKeyboardNotifications() {
    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "keyboardWillShow:",
        name: UIKeyboardWillShowNotification,
        object: nil)

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: "keyboardWillBeHidden:",
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func keyboardWillShow(notification: NSNotification) {
    if (!_keyboardActivated) {
      // do stuff
    }
    _keyboardActivated = true
}

func keyboardWillBeHidden(notification: NSNotification) {
    _keyboardActivated = false
}
}

Set delegate of your textfield and implement the below methods 设置文本字段的委托并实现以下方法

func textFieldDidBeginEditing(textField: UITextField) {
        animateViewMoving(true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
        animateViewMoving(false, moveValue: 100)
}

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

Reference: http://www.jogendra.com/2015/01/uitextfield-move-up-when-keyboard.html 参考: http : //www.jogendra.com/2015/01/uitextfield-move-up-when-keyboard.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM