ios - 出现键盘时快速移动 View

标签 ios swift keyboard uitextfield

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

这就是我显示键盘和移动 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
            }
    }

最佳答案

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

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

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

关于如何在选择 UITextField 时管理键盘的引用:https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

关于ios - 出现键盘时快速移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35089529/

相关文章:

arrays - 尝试根据在另一个下拉菜单中选择的值快速过滤一个下拉菜单中的值

Objective-C for 循环到 Swift 3

ios - CLLocationManager 的 pausesLocationUpdatesAutomatically 属性在 Swift 中始终为 false (iOS8/iOS7)

ios - Realm realmWithPath 方法在 iOS 上崩溃

ios - 动态宽度大小以适应 stackview 中的控件

emacs - emacs 的 Xkb 配置

windows - 将 Win32 API WndProc Key 消息从一个窗口传输到另一个窗口

dart - 如何在 flutter 中检测选定的键盘语言?

python - 如何使用 peewee/mysqldb 通过 python 查询 MySQL 数据库?

iphone - 调整 UIPickerView 的大小