键盘出现时快速移动视图

时间:2016-01-29 16:37:01

标签: ios swift keyboard uitextfield

我在视图中有六个UITextFields,我想决定视图是否必须移动。在移动视图之前,如何检查选择了哪个TextField?

这是我的代码显示键盘和移动视图:

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

1 个答案:

答案 0 :(得分:3)

如果您想知道选择了哪个UITextField,可以使用textFieldDidBeginEditingtextFieldDidEndEditing

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

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

有关如何在选择UITextFieldhttps://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

时管理键盘的参考