I've seen posts around here that suggest that UIScrollViews
should automatically scroll if a subview UITextField
becomes the first responder; however, I can't figure out how to get this to work.
What I have is a UIViewController
that has a UIScrollView
and within the UIScrollView
there are multiple textfields.
I know how to do this manually if necessary; however, from what I've been reading, it seems possible to have it autoscroll. Help please.
I hope this example will help you You can scroll to any point by this code.
So if you have textfield, it must have some x,y position on view, so you can use
This should do the trick,
But if you don't know when to call this code, so you should learn UITextFieldDelegate methods
Implement this method in your code
I hope you know how to use delegate methods.
I know this question has already been answered, but I thought I would share the code combination that I used from @Adeel and @Basil answer, as it seems to work perfectly for me on iOS 9.
I also used the animated method, it makes for a much smoother transition.
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
UITextField
. See below for solutionUIScrollView
somewhere in the view hierarchy between theUITextField
and theUIScrollView
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.As Michael McGuire mentioned in his point #2 above, the system's default behavior misbehaves when the scroll view contains another scroll view between the text field and the scroll view. I've found that the misbehavior also occurs when there's a scroll view merely next to the text field (both embedded in the scroll view that needs to be adjusted to bring the text field into view when the text field wants to start editing. This is on iOS 12.1.
But my solution is different from the above. In my top-level scroll view, which is sub-classed so I can add properties and override methods, I override
scrollRectToVisible:animated:
. It simply calls its[super scrollRectToVisible:animated:]
unless there's a property set that tells it to adjust therect
passed in, which is the frame of the text field. When the property is non-nil, it is a reference to theUITextField
in question, and therect
is adjusted so that the scroll view goes further than the system thought it would. So I put this in theUIScrollView
's sub-classed header file:(with appropriate
@synthesize textFieldToBringIntoView;
in the implementation. Then I added this override method to the implementation:In the delegate method for the
UITextField
for when it's about to begin editing, just settextFieldToBringIntoView
to thetextField
in question:It seems to work. And if Apple fixes their bug, it seems like it might still work (fingers crossed).
If you have multiple textfields say
Textfield1
,Textfield2
,Textfield3
and you want to scroll the scrollview along the y-axis when textfield2 becomes first responder: