Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How programmatically move a UIScrollView to focus in a control above keyboard?

I have 6 UITextFields on my UIScrollView. Now, I can scroll by user request. But when the keyboard appear, some textfields are hidden.

That is not user-friendly.

How scroll programmatically the view so I get sure the keyboard not hide the textfield?

like image 789
mamcx Avatar asked Jan 27 '09 19:01

mamcx


2 Answers

Here's what worked for me. Having an instance variable that holds the value of the UIScrollView's offset before the view is adjusted for the keyboard so you can restore the previous state after the UITextField returns:

//header @interface TheViewController : UIViewController <UITextFieldDelegate> {     CGPoint svos; }   //implementation - (void)textFieldDidBeginEditing:(UITextField *)textField {     svos = scrollView.contentOffset;     CGPoint pt;     CGRect rc = [textField bounds];     rc = [textField convertRect:rc toView:scrollView];     pt = rc.origin;     pt.x = 0;     pt.y -= 60;     [scrollView setContentOffset:pt animated:YES];            }  - (BOOL)textFieldShouldReturn:(UITextField *)textField {     [scrollView setContentOffset:svos animated:YES];      [textField resignFirstResponder];     return YES; } 
like image 63
james_womack Avatar answered Sep 28 '22 05:09

james_womack


Finally, a simple fix:

UIScrollView* v = (UIScrollView*) self.view ; CGRect rc = [textField bounds]; rc = [textField convertRect:rc toView:v]; rc.origin.x = 0 ; rc.origin.y -= 60 ;  rc.size.height = 400; [self.scroll scrollRectToVisible:rc animated:YES]; 

Now I think is only combine this with the link above and is set!

like image 29
mamcx Avatar answered Sep 28 '22 03:09

mamcx