UITextview的contentInset& scrollIndicatorInsets with keyboard&在Swift中旋转?

时间:2015-07-05 07:50:18

标签: swift uitextview

我正在使用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并没有随着我的新身高而改变。那我该怎么办?谢谢!

注意:视图顶部有一个导航栏

1 个答案:

答案 0 :(得分:2)

考虑UIKeyboardDidChangeFrameNotification通知。它是在键盘的框架刚刚更改时发送的。实际上,您应该使用UIKeyboardDidChangeFrameNotification而不是UIKeyboardWillShowNotification