ios - 在 iOS 记事本应用程序中自动滚动

标签 ios uiscrollview uikeyboard

当我开始编辑本会被键盘遮盖的文本时,我对记事本应用程序中的自动滚动感到好奇。我了解一般情况:应用程序使用光标的位置、键盘高度和一些偏移量来滚动记事本。但是我不清楚究竟是什么对象/值在改变。

在框架、边界 contentOffsets 等方面发生了什么?

编辑:我对这种自动滚动的一般工作原理很感兴趣。记事本应用就是一个很好的例子。

最佳答案

这在 Text, Web, and Editing Programming Guide for iOS 中有描述:

When asked to display the keyboard, the system slides it in from the bottom of the screen and positions it over your application’s content. Because it is placed on top of your content, it is possible for the keyboard to be placed on top of the text object that the user wanted to edit. When this happens, you must adjust your content so that the target object remains visible.

Adjusting your content typically involves temporarily resizing one or more views and positioning them so that the text object remains visible. The simplest way to manage text objects with the keyboard is to embed them inside a UIScrollView object (or one of its subclasses like UITableView). When the keyboard is displayed, all you have to do is reset the content area of the scroll view and scroll the desired text object into position. Thus, in response to a UIKeyboardDidShowNotification, your handler method would do the following:

  1. Get the size of the keyboard.
  2. Adjust the bottom content inset of your scroll view by the keyboard height.
  3. Scroll the target text field into view.

关于ios - 在 iOS 记事本应用程序中自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8661442/

相关文章:

iOS iPhone - 在 keyboardWillShow 之前获取 UIKeyboard 高度

ios - UICollectionViewCell 拖动动画

objective-c - iOS 在设备旋转时更改自动布局约束

ios - 在 App 的整个生命周期中仅显示一次 View

ios - 向多个 TextView 添加排除路径

iphone - 为什么在编辑 UITextField (在 UITableViewCell 中)时不会出现 UITableView 'Auto-scroll' ? (iPhone)

ios - React-native Javascript 通过 codepush 进行的更改未反射(reflect)在 iPhone 上

ios - UIACollectionView 单元格与 visibleCells

ios - 从 UIScrollView 后面的取消按钮获取操作

ios - 当键盘存在时如何使 UITextField 向上移动 - 开始编辑?