Scrolling through the text at startup. Editing and keyboard appear

I have a viewController consisting of a UIImageView that fills the entire view and two text fields located in the center of the view. All this is inside ScrollView. I use Storyboard and I have disabled autorun. When I click on textField and open the keyboard, I would like the scroll to shift directly to textField. How can i do this?

+2
source share
5 answers

Note that your TextField library is named txtField and then UITextField Delegate using

 txtField.delegate = self; 

Given that your ScrollView socket is called scrollView . Implement the Textfield delegate method:

 - (void)textFieldDidBeginEditing:(UITextField *)textField { NSLog(@"%@",NSStringFromCGRect(textField.frame)); [scrollView scrollRectToVisible:textField.frame animated:YES]; } 

Hope this helps.

Let me know if you need more help.

+4
source

Add your controller as an observer from keyboard notifications. When you receive a notification, resize the scroll frame and / or adjust the offset of the content (depending on the exact effect and size of the content).

0
source

Here are a few options I used before:

0
source

You can convert the text field coordinate system to a scroll coordinate system. Use the code below

 -(void)rearrangeView{ // previous if (txtActiveField == txtUsername) { CGPoint pt; CGRect rc = [txtUsername bounds]; rc = [txtUsername convertRect:rc toView:scrollViewLogin]; pt = rc.origin; pt.x = 0; pt.y -= 40; [scrollViewLogin setContentOffset:pt animated:YES]; } else if (txtActiveField == txtPassword) { // [self.txtEmail becomeFirstResponder]; CGPoint pt; CGRect rc = [txtPassword bounds]; rc = [txtPassword convertRect:rc toView:scrollViewLogin]; pt = rc.origin; pt.x = 0; pt.y -= 40; [scrollViewLogin setContentOffset:pt animated:YES]; } } #pragma mark- UITextField Delegate -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { txtActiveField = textField; //txtActiveField.placeholder = @""; [self rearrangeView]; return YES; } 

Here txtActiveField is an instance variable of type UItextField

0
source

All Articles