user1930931
user1930931

Reputation: 303

How to add a scroll view to an ios app

I have a tabbed application that I would like to have a scroll view on. I already have a few text fields and labels on that tab. The problem is, the keyboard hides some of the text fields. How would I add a vertical scroll to prevent that?

Upvotes: 0

Views: 193

Answers (2)

digidigo
digidigo

Reputation: 2584

You can accomplish what you are looking for without a scrollview.

In you delegate method for you textfield, You can change the frame of the viewController's view.

something like

- (void)textFieldDidBeginEditing:(UITextField *)textField;           
{
CGRect newFrame = self.view.frame;
newFrame.origin.y = - 40;  // move the view up to the point your textfield is visible
self.view.frame = newFrame;
}

Then in Set it back

- (void)textFieldDidEndEditing:(UITextField *)textField;  
{
 CGRect newFrame = self.view.frame;
 newFrame.origin.y = 0;
 self.view.frame = newFrame;   
}

Usually I animate this with 0.33 seconds duration.

Upvotes: 1

Ismael
Ismael

Reputation: 3937

The solution requires quite a bit of code, but here's the general idea of what you need:

You will need to add the text fields (and everything else for consistency) to a scrollView.

You need to setup the scroll view to have vertical scrolling space only, but set the scrollEnabled to false so that the user can't scroll it manually.

Then you need to listen to UIKeyboardWillShowNotification and UIKeyboardWillHideNotification and manually scroll it up/down as required.

Upvotes: 0

Related Questions