With the iOS SDK:
I have a UIView
with UITextField
s that bring up a keyboard. I need it to be able to:
Allow scrolling of the contents of the
UIScrollView
to see the other text fields once the keyboard is brought upAutomatically "jump" (by scrolling up) or shortening
I know that I need a UIScrollView
. I've tried changing the class of my UIView
to a UIScrollView
but I'm still unable to scroll the textboxes up or down.
Do I need both a UIView
and a UIScrollView
? Does one go inside the other?
What needs to be implemented in order to automatically scroll to the active text field?
Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.
Note: the UIView
(or UIScrollView
) that I'm working with is brought up by a tabbar (UITabBar
), which needs to function as normal.
Edit: I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.
I've got it working where I change the frame size of the UIScrollView
when the keyboard goes up and down. I'm simply using:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50); //resize
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//keyboard will hide
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height + 215 - 50); //resize
}
However, this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.
Note: this answer assumes your textField is in a scrollView.
I prefer to deal with this using scrollContentInset and scrollContentOffset instead of messing with the frames of my view.
First let's listen for the keyboard notifications
Next step is to keep a property that represents the current first responder (UITextfield/ UITextVIew that currently has the keyboard).
We use the delegate methods to set this property. If you're using another component, you will need something similar.
Note that for textfield we set it in didBeginEditing and for textView in shouldBeginEditing. This is because textViewDidBeginEditing gets called after UIKeyboardWillShowNotification for some reason.
Finally, here's the magic
Easiest solution found
There so many solutions, but I've spend some hours before it start works. So, I put this code here (just paste to the project, any modifications needn't):
P.S: I hope the code help somebody make desired effect quickly. (Xcode 4.5)
When
UITextField
is in aUITableViewCell
scrolling should be setup automatically.If it is not it is probably because of incorrect code/setup of the tableview.
For example when i reloaded my long table with one
UITextField
at the bottom as follows,then my textfield at the bottom was obscured by the keyboard which appeared when I clicked inside the textfield.
To fix this I had to do this -
I've put together a universal, drop-in
UIScrollView
,UITableView
and evenUICollectionView
subclass that takes care of moving all text fields within it out of the way of the keyboard.When the keyboard is about to appear, the subclass will find the subview that's about to be edited, and adjust its frame and content offset to make sure that view is visible, with an animation to match the keyboard pop-up. When the keyboard disappears, it restores its prior size.
It should work with basically any setup, either a
UITableView
-based interface, or one consisting of views placed manually.Here' tis: solution for moving text fields out of the way of the keyboard
RPDP's code successfully moves the text field out of the way of the keyboard. But when you scroll to the top after using and dismissing the keyboard, the top has been scrolled up out of the view. This is true for the Simulator and the device. To read the content at the top of that view, one has to reload the view.
Isn't his following code supposed to bring the view back down?