I want to make my uiviewcontroller.xib
scroll. My view controller has like 8 textfields. So my problem is when I want to write something in the 5th textfield
and so on my keyboard covers the textfields. How can I get rid of this problem, and make my viewcontroller scroll?
Please guide in detail because am new to iPhone development.
Thanks in advance.
Select the text fields and go to the Editor menu, and choose Embed In -> Scroll View. This will automatically place a scroll view in your view hierarchy and move the text fields into it.
Also often when people want to have multiple text fields on screen and have them scroll, they will actually place them in a UITableView, where each cell has a text field in it. Usually this is with the grouped table view cell style. If you do it this way, the table view can handle automatically adjusting its size when the keyboard appears, so that you don't have to handle this yourself.
This is how you do it:
All the previous answers are great, but if you don't want to confuse yourself with code, use a control.
One that I love is: https://github.com/michaeltyson/TPKeyboardAvoiding (Cocoapods: pod 'TPKeyboardAvoiding')
All you have to do is embed your text fields into a UIScrollView (Editor/Embed In. Select your UITextFields first) then set that UIScrollView's class to TPKeyboardAvoidingScrollView.
Yes you should learn how to do it manually first, but after just use this if you'd like.
You can use a ScrollView.
Adding the scroll view
Drag an drop a scrollView onto your view controller, the same way you would with a text field and adjust the dimensions to suit your needs (it seems like you'd want it to fill the view controller.)
Then place the text fields into the scroll view. I think it's easiest to do using the document outline on the left. Drag the text fields onto the scroll view here, like in the picture.
Making the scroll view scroll when keyboard appears
Add this code to your view controller in
viewDidLoad
And add these methods to your view controller
The first two of these methods is called when the keyboard is shown. The second is called when you start to edit a text field.
Now go to your storyboard and attach actions of the text fields to the method that was just added. You can right click on the text field, select the appropriate action and drag it to the method.
Your should see something like this when you right click on your textfields.
Add this property to your view controller and right click drag from your scroll view to it. It allows your view controller to control the scroll view.
Like this:
Closing the keyboard
When the return button is pressed we want the keyboard to close.
In your view controller header make your view controller a
UITextFieldDelegate
Like this:Add this code to your view controller in
viewDidLoad
And add these methods to your view controller
The first method is called when the keyboard is closed. It returns the scroll view to its original position. The second method is called when you have finished editing a text field. It allows the keyboard to be dismissed when this happens.
More info
Here is more information on managing the keyboard.
And for reference here is my ViewController.h
and ViewController.m