[英]Keyboard hide UITextView swift 2
I m using UITableView
with 2 customs cells, I have custom cells with UITextView
and UITextField
, I'm trying to move up the edited field when it's hiding by the keyboard on the top of the keyboard, here is my code for the viewDidLoad
: 我使用带有2个海关单元的
UITableView
,我有UITextView
和UITextField
自定义单元格,当它被键盘顶部的键盘隐藏时,我试图向上移动编辑的字段,这是我的viewDidLoad
代码:
override func viewDidLoad() {
super.viewDidLoad()
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillShowNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillHideNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(adjustForKeyboard), name: UIKeyboardWillChangeFrameNotification, object: nil)
}
and here the function called when keyboard notification is sent : 这里是发送键盘通知时调用的函数:
func adjustForKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)
if notification.name == UIKeyboardWillHideNotification {
myTableView.contentInset = UIEdgeInsetsZero
print("ZERO")
} else {
myTableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
}
myTableView.scrollIndicatorInsets = myTableView.contentInset
}
It's working perfectly for the UITextField
but not for the UITextView
. 它适用于
UITextField
但不适用于UITextView
。 Why? 为什么?
As answered by Steve ( dismiss keyboard with a uiTextView ) 由史蒂夫回答( 用uiTextView解雇键盘 )
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.