【问题标题】:UITextview's contentInset & scrollIndicatorInsets with keyboard & rotation in Swift?UITextview 的 contentInset 和 scrollIndicatorInsets 在 Swift 中带有键盘和旋转?
【发布时间】:2015-07-05 07:50:17
【问题描述】:

我正在使用 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 并没有随着我的新高度而改变。那么我能做什么呢?谢谢!

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

【问题讨论】:

    标签: swift uitextview


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-12-20
      • 1970-01-01
      • 2011-10-31
      • 2019-03-11
      • 1970-01-01
      • 2012-04-02
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多