我正在使用 UITextView 并在键盘显示/隐藏时设置它的 contentInset 和 scrollIndicatorInsets,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardHide:", name: UIKeyboardWillHideNotification, object: nil)
}
override func shouldAutorotate() -> Bool {
return !self.keyboardShowing
}
func keyboardShow(n:NSNotification) {
self.keyboardShowing = true
let d = n.userInfo!
var r = (d[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
r = self.tv.convertRect(r, fromView:nil)
self.tv.contentInset.bottom = r.size.height
self.tv.scrollIndicatorInsets.bottom = r.size.height
}
func keyboardHide(n:NSNotification) {
self.keyboardShowing = false
self.tv.contentInset = UIEdgeInsetsZero
self.tv.scrollIndicatorInsets = UIEdgeInsetsZero
}
它工作正常,但是当我旋转我的设备时,scrollIndicatorInsets 和 contentInset 没有随着我的新高度而改变。那我该怎么办?谢谢!
注意: View 顶部有一个导航栏
最佳答案
考虑 UIKeyboardDidChangeFrameNotification
通知。当键盘的框架刚刚改变时发送。实际上,您应该使用 UIKeyboardDidChangeFrameNotification
而不是使用 UIKeyboardWillShowNotification
。
关于swift - UITextview 的 contentInset 和 scrollIndicatorInsets 与 Swift 中的键盘和旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31228230/