How to make a UIScrollView auto scroll when a UITextField becomes a first responder

前端 未结 11 1803
有刺的猬
有刺的猬 2021-01-31 03:24

I\'ve seen posts around here that suggest that UIScrollViews should automatically scroll if a subview UITextField becomes the first responder; however,

11条回答
  •  一整个雨季
    2021-01-31 03:49

    There is nothing you have to do manually. It is the default behavior. There are two possibilities as to why you are not seeing the behavior

    1. The most likely reason is that the keyboard is covering your UITextField. See below for solution
    2. The other possibility is that you have another UIScrollView somewhere in the view hierarchy between the UITextField and the UIScrollView that you want to auto scroll. This is less likely but can still cause problems.

    For #1, you want to implement something similar to Apple's recommendations for Moving Content That Is Located Under the Keyboard. Note that the code provided by Apple does not account for rotation. For improvements on their code, check out this blog post's implementation of the keyboardDidShow method that properly translates the keyboard's frame using the window.

提交回复
热议问题