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?
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; }
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With