我已创建的UIScrollView
的延伸,其中当用户选择一个文本字段和出现的键盘,文本字段会向上滚动,如果它是在键盘的方式。我有工作了UITextField
,但它似乎并没有与UITextView
工作。我已经搜索了很多帖子在stackoverflow,但似乎无法找到任何帮助。下面是该扩展的代码:的UITextView避免键盘滚动
extension UIScrollView {
func respondToKeyboard() {
self.registerForKeyboardNotifications()
}
func registerForKeyboardNotifications() {
// Register to be notified if the keyboard is changing size i.e. shown or hidden
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(keyboardWasShown(_:)),
name: UIKeyboardWillShowNotification,
object: nil
)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(keyboardWillBeHidden(_:)),
name: UIKeyboardWillHideNotification,
object: nil
)
}
func keyboardWasShown(notification: NSNotification) {
if let info = notification.userInfo,
keyboardSize = info[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.size {
self.contentInset.bottom = keyboardSize.height + 15
self.scrollIndicatorInsets.bottom = keyboardSize.height
var frame = self.frame
frame.size.height -= keyboardSize.height
}
}
func keyboardWillBeHidden(notification: NSNotification) {
self.contentInset.bottom = 0
self.scrollIndicatorInsets.bottom = 0
}
在我的视图控制器我只想把它想:
scrollView.respondToKeyboard()
有人能指出我的我怎么能实现UITextView
作为一个正确的方向如果键盘挡道,扩展可以向上移动?
感谢您的链接。所以,现在我有** UIScrollView **的扩展,我应该更改扩展名,以便它是** UITextView **和** UITextField **的扩展? @iOS Geek – coderdojo
只是认为它可能是一种更好的方式,而不是 – coderdojo
@coderdojo - 如果您在视图控制器中使用委托处理键盘,则不需要扩展。 –