UIKeyboardWillShowNotification,iOS8 和安全文本输入

UIKeyboardWillShowNotification, iOS8 and secure text entry

我有一个带有 "secure text entry" 的表单,我想在打开键盘时向上滚动我的视图。

我正在收听 UIKeyboardWillShowNotification 但是当焦点位于密码字段时,会再次发送此通知并且我的视图会再次向上滚动一次。

有没有办法避免这个问题?

第一个解决方案:使用 UITableView.

第二种解决方案:不要在键盘显示时盲目滚动。在滚动视图之前检查视图的框架或内容偏移量。使该功能更加可靠。

我建议不要移动 UIScrollView tbh 的偏移量。最好像这样更改 contentInset:

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size.height {
            scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0)
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size.height {
            scrollView.contentInset = UIEdgeInsetsZero
        }
    }
}

您可能仍想 fiddle 在键盘出现时调整 contentInstent 高度。但是,当您执行此操作时,它不会移动视图,而只是创建键盘所需的 space 并且用户感觉不那么刺耳。