简体   繁体   English

键盘出现时快速移动视图

[英]Swift move view when keyboard appears

I have six UITextFields in my view and I want to decide whether the view has to move or not. 我在视图中有六个UITextFields,我想决定视图是否必须移动。 How can I check which TextField is selected before moving my view? 在移动视图之前,如何检查选择了哪个TextField?

Thats my Code for showing the keyboard and moving the view: 这是我的代码显示键盘和移动视图:

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

func keyboardWillShow(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                self.view.frame.origin.y -= keyboardSize.height - 85
            }
    }

    func keyboardWillHide(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                self.view.frame.origin.y += keyboardSize.height - 85
            }
    }

If you want to know which UITextField is selected, you can use textFieldDidBeginEditing and textFieldDidEndEditing 如果您想知道选择了哪个UITextField ,可以使用textFieldDidBeginEditingtextFieldDidEndEditing

func textFieldDidBeginEditing(textField: UITextField!) {
    currentSelectedTextField = textField
}

func textFieldDidEndEditing(textField: UITextField!) {
    currentSelectedTextField = nil
}

For your reference about how to manage keyboard when selecting a UITextField : https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html 有关如何在选择UITextField时管理键盘的参考: https//developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

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

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