键盘上下移动

时间:2016-11-10 04:54:25

标签: ios view keyboard uitextfield swift3

我有一个loginView,里面有两个textFields和一个按钮。我必须在点击textField的同时向上移动视图,并在按下返回键时向下移动视图。

我的问题是它适用于所有条件,但是当点击一个textField视图正在向上移动但同时我们去下一个textField视图正在向下移动。

import UIKit
class CheckFontIconView: UIViewController,UITextFieldDelegate {

    var activeField: UITextField?

    @IBOutlet weak var loginFieldsView: UIView!

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var mobileNo: UITextField!
    @IBOutlet weak var textFieldPassword: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        mobileNo.delegate = self
        textFieldPassword.delegate = self

        registerForKeyboardNotifications()
    }

    @IBAction func btnLoginAction(_ sender: Any) {

    }

    deinit {

        //NotificationCenter.default.removeObserver(self)
        self.deregisterFromKeyboardNotifications()
    }


    func registerForKeyboardNotifications() {

        //Adding notifies on keyboard appearing
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func deregisterFromKeyboardNotifications() {

        //Removing notifies on keyboard appearing
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func keyboardWasShown(notification: NSNotification) {

        //Need to calculate keyboard exact size due to Apple suggestions
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        if self.activeField != nil {
            self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
        }
    }

    func keyboardWillBeHidden(notification: NSNotification) {

        //Once keyboard disappears, restore original positions
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        self.loginFieldsView.frame.origin.y -= (keyboardSize?.height)!
        self.loginFieldsView.endEditing(true)
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.activeField = textField
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        self.activeField = nil

    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        activeField?.resignFirstResponder()
        return true
    }
}

2 个答案:

答案 0 :(得分:0)

感谢您提问...

使用TPKeyboard来实现上述功能,然后不管理这类内容(通过TPKeyboard自动管理)......

<强>更新

我认为你在以下代码中犯了一些错误......

func textFieldDidEndEditing(_ textField: UITextField) {
        self.activeField = nil
 }

您必须进行调查并确定确切的问题..

快乐的编码......

答案 1 :(得分:0)

只需一次更改就可以在变量中保留您的视图旧框架,并在键盘消失时再次将其分配给您的视图。

import UIKit
    class CheckFontIconView: UIViewController,UITextFieldDelegate {

var activeField: UITextField?

@IBOutlet weak var loginFieldsView: UIView!

@IBOutlet weak var label: UILabel!
@IBOutlet weak var mobileNo: UITextField!
@IBOutlet weak var textFieldPassword: UITextField!
var previousFrame : CGRect!
override func viewDidLoad() {
    super.viewDidLoad()

    previousFrame=self.loginFieldsView.frame

    mobileNo.delegate = self
    textFieldPassword.delegate = self

    registerForKeyboardNotifications()
}

func keyboardWasShown(notification: NSNotification) {

    //Need to calculate keyboard exact size due to Apple suggestions
    var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
    if self.activeField != nil {
        self.loginFieldsView.frame.origin.y = previousFrame.origin.y-(keyboardSize?.height)!
    }
}

func keyboardWillBeHidden(notification: NSNotification) {

    //Once keyboard disappears, restore original positions
    /*var info = notification.userInfo!
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size*/
    self.loginFieldsView.frame = previousFrame
    //self.loginFieldsView.endEditing(true)
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    self.activeField = textField
}

func textFieldDidEndEditing(_ textField: UITextField) {
    self.activeField = nil

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    activeField?.resignFirstResponder()
    return true
}
}