【问题标题】:UITextView not shifting up with the keyboardUITextView 不随键盘向上移动
【发布时间】:2018-01-12 17:00:29
【问题描述】:

我花了一天的大部分时间来解决这个问题,这是我的问题:

我有一个UIScrollView,里面有一个垂直堆栈视图,在堆栈视图里面我有几个UIViews,其中包含不同的东西。在底部视图中,我有一个UITextView,理想情况下,我希望在开始输入时向上移动,这样我就可以实际看到我正在输入的内容。

如果我在最后一个视图中有 UITextField 而不是 @987654331 @,但我真的很想拥有返回键/多行功能,这似乎需要UITextView。

这是我在代码方面的内容(根据上述几个链接):

func registerForKeyboardNotifications() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
}

func keyboardWasShown(_ notification: NSNotification) {

    guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }

    let keyboardFrame = keyboardFrameValue.cgRectValue
    let keyboardSize = keyboardFrame.size

    let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

}

func keyboardWillBeHidden(_ notification: NSNotification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

我在viewDidLoad() 中调用了registerForKeyboardNotifications(),当我使用UITextField 时,这完美,但无论出于何种原因,该功能不适用于UITextView .我能找到的与我的问题最接近的 SO 问题是 this one,但这个问题从来没有用涉及 UITextView 的可行解决方案来回答。我看到有几篇帖子提到了IQKeyboardManager,但我希望能在本地解决这个问题。

【问题讨论】:

    标签: swift3 uiscrollview uitextview


    【解决方案1】:

    我找到了一个结合了contentInset 和setContentOffset 的解决方案,但我敢肯定它不是最优雅的解决方案。但是,如果其他人无法将textView 升级为keyBoardWasShown,您可以执行以下操作:

    设置您的viewController 以通知键盘何时出现;

    func registerForKeyboardNotifications() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: .UIKeyboardDidShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: .UIKeyboardWillHide, object: nil)
    }
    
    func keyboardWasShown(_ notification: NSNotification) {
    
        guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
    
        let keyboardFrame = keyboardFrameValue.cgRectValue
        let keyboardSize = keyboardFrame.size
    
        let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets
    
    }
    
    func keyboardWillBeHidden(_ notification: NSNotification) {
        let contentInsets = UIEdgeInsets.zero
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets
    }
    

    请务必在viewDidLoad 中致电registerForKeyboardNotifications()。

    这将根据您的特定设置而有所不同。因为我的view 覆盖了大部分屏幕空间,所以我真的只希望我的scrollView(我把它变成IBOutlet)跳起来足以显示我的textView 的一部分,这样用户就可以知道它在那里并且可编辑。这导致对上述处理键盘显示/隐藏状态的代码部分进行了以下更改;

    func keyboardWasShown(_ notification: NSNotification) {
    
        guard let info = notification.userInfo, let keyboardFrameValue = info[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
    
        let keyboardFrame = keyboardFrameValue.cgRectValue
        let keyboardSize = keyboardFrame.size
    
        let contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height - 50.0, 0.0)
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets
    
        let bottom = keyboardSize.height - 60.0
        scrollView.setContentOffset(CGPoint(x: 0, y: bottom), animated: true)
    
    }
    
    func keyboardWillBeHidden(_ notification: NSNotification) {
    
        let contentInsets = UIEdgeInsets.zero
        scrollView.contentInset = contentInsets
        scrollView.scrollIndicatorInsets = contentInsets
    
    }
    

    关键项目是scrollView.setContentOffset 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2021-11-30
      • 2013-08-15
      • 2021-03-03
      • 2018-06-18
      • 2011-09-22
      相关资源
      最近更新 更多