Update
It appears that the content offset is only set as a consequence of calling UITextField.resignFirstResponder()
when the main scrollview is scrolled by a certain amount. In other words, there's a threshold. If the content offset is underneath the threshold calling resignFirstResponder()
won't result in the content offset being set, and if above it, then it will be set to it. In other words it appears that UIKit has a problem with the content offset being higher than X, and if it is at the time the keyboard is dismissed, then it is set to X.
Original
I have a UIScrollView
that is normally the full width and height of the screen. It is constrained to the top anchor of the keyboard layout guide. So when the keyboard pops up, this scrollview's frame shrinks by the height of the keyboard.
Inside this scrollview is another scrollview. It contains a stackview that hosts the UITextFields
. The fields are scrollable, and the main scrollview is only scrollable when the keyboard is displayed.
The problem occurs when the keyboard is being dismissed. I'm trying to access the content offset of the main scrollview at the time keyboard begins to hide. I do this using keyboard will hide/show notifications. However, at this point in time the content offset has been set to something different than it was at the time the user dismissed the keyboard.
I narrowed down the problem to the UITextField.resignFirstResponder()
method. I overrode the contentOffset
property of the main scrollview and found using breakpoints that when resignFirstResponder()
is called on the focused textfield, the content offset of the scrollview is changed by UIKit.
What I want is for UIKit to stop changing the content offset when UITextField.resignFirstResponder()
is called. This way I can get the content offset at the time the user dismissed the keyboard from within the keyboard will hide callback.
I'll create a minimal reproducible example if really required, but I thought I'd take a shot and see if this issue rings a bell for anyone that already knows a solution to it.