UITextview 的 contentInset 和 scrollIndicatorInsets 与键盘和旋转 Swift?
UITextview's contentInset & scrollIndicatorInsets with keyboard & rotation in Swift?
我正在使用 UITextView 并在键盘为 show/hide 时设置它的 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 没有随着我的新高度而改变。那我该怎么办?谢谢!
注意:视图顶部有一个导航栏
正在考虑 UIKeyboardDidChangeFrameNotification
通知。当键盘的框架刚刚改变时发送。实际上,您应该使用 UIKeyboardDidChangeFrameNotification
而不是 UIKeyboardWillShowNotification
.
我正在使用 UITextView 并在键盘为 show/hide 时设置它的 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 没有随着我的新高度而改变。那我该怎么办?谢谢!
注意:视图顶部有一个导航栏
正在考虑 UIKeyboardDidChangeFrameNotification
通知。当键盘的框架刚刚改变时发送。实际上,您应该使用 UIKeyboardDidChangeFrameNotification
而不是 UIKeyboardWillShowNotification
.