uitextview contentinset默认动画

发布于 2025-02-12 11:59:34 字数 780 浏览 3 评论 0原文

当要显示键盘时,我有此代码可以调整UITEXTVIEW。

  @objc func keyboardWillShow(notification: NSNotification) {
  if let rectValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
    let keyboardSize = rectValue.cgRectValue.size
      
      let contentInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height - view.safeAreaInsets.bottom, right: 0)
      
      textView.contentInset = contentInsets
      textView.scrollIndicatorInsets = contentInsets
     
  }
} 

我看到设置ContentInset自动创建一个滚动动画(键盘完全出现后),使光标处的文本可见。动画会自动发生,无法将其禁用。当键盘显示时,我想滚动到文本视图中的给定位置(最好将其与键盘外观一起使用)。隐式contentInset动画使其不可能。首先,键盘动画,然后ContentInset动画,现在只能在TextView中使用我们选择的位置动画。这很尴尬。我想知道实现它的正确方法是什么?

I have this code to adjust UITextView when keyboard is about to be displayed.

  @objc func keyboardWillShow(notification: NSNotification) {
  if let rectValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
    let keyboardSize = rectValue.cgRectValue.size
      
      let contentInsets: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height - view.safeAreaInsets.bottom, right: 0)
      
      textView.contentInset = contentInsets
      textView.scrollIndicatorInsets = contentInsets
     
  }
} 

I see setting contentInset automatically creates a scrolling animation (after the keyboard appears fully) that makes the text at the cursor visible. The animation happens automatically and there is no way to disable it. I want to scroll to a given location in the textView sometimes when the keyboard is presented (preferably, animating it along with keyboard appearance). The implicit contentInset animation makes it impossible however. First the keyboard animates, then contentInset animates, and now only can I animate to my chosen location in textView. This is awkward. I am wondering what is the right way to achieve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文