0

i am having 10 textfields over the scrollView. I want to scroll the view when keyboard Appears on Textfileds . Here i am using the below code to achieve this. But it is moving the screen up from the First Textfield my requirement is i want to move view up when keyboard come over the textfield only otherwise i don't want to move the view up. Please help me.

- (void)textFieldDidBeginEditing:(UITextField *)textField { CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y); [myScrollView setContentOffset:scrollPoint animated:YES]; } 
3
  • let me get this straight: what's the unwanted behaviour here? that the view scrolls even when the first text field is selected and you don't want to? Commented Mar 7, 2014 at 14:21
  • The unwanted behaviour I think is that it ALWAYS Scrolls the view even when the keyboard doesn't overlap the currentResponder. Commented Mar 7, 2014 at 14:25
  • @NicolaMiotto Here the view is scrolling up when i click my first textField which i do not want. I want to scroll view up only if keyboard covers the TextFields Commented Mar 7, 2014 at 14:29

3 Answers 3

1

Something like this maybe? (You may have to readapt it for landscape mode)

- (void)textFieldDidBeginEditing:(UITextField *)textField { CGFloat keyboardHeight = 216.0; CGFloat viewHeight = myScrollView.frame.size.height; BOOL covering = (textField.frame.origin.y - myScrollView.contentOffset.y) > (viewHeight - keyboardHeight); if(covering) { CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y); [myScrollView setContentOffset:scrollPoint animated:YES]; } } 
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your reply. view is going up even keyboard not covering the Textfields in 4 inch screen.
In 3.5 inch screen the view is moving up when cover the textfield and then when i click on next textfield it is coming down. when i again click onother textfield it is moving up....
For the 4 inch screen I would check that the scroll view has the same height as the parent view (in that case the viewHeight variable will get the right value. Then: what do you mean with "it is coming down"? That you scroll up the view while the keyboard is there, you select the another text field and then it is scrolled down behind the keyboard?
Mitto. Sorry because of my code mistake it happend like that(it is coming down)
i have a uiview with the frame size 10, 60, 300, 350. placing the myScrollview on the view with frame (0, 0, 320, 350), content size (300, 350). I am having textfields on scrollview.
|
0

There are ready solutions for this.

You could use TPKeyboardAvoiding to accomplish your mission.

Comments

0

Use keyboard notifications to be alerted when the keyboard is presented e.g.:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

(Also remove these in viewWillDissapear:

[[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification]; [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification]; 

Then in the functions respectively:

- (void)keyboardWillShow:(NSNotification *) notification { CGRect appFrame = YOUR_VIEWS_FRAME CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrame = [self.view.window convertRect:keyboardFrame toView:self.view]; UITextField *current = THE_CURRENT_RESPONDER CGRect relativeFrame = [self.view convertRect:current.frame fromView:current.superview]; float difference = 0.0; //Here we give it a 20 pixel padding between the top of the keyboard and the bottom of the text field. if (CGRectIntersectsRect(relativeFrame, keyboardFrame)) difference = appFrame.size.height - keyboardFrame.size.height - 20 - relativeFrame.size.height - current.frame.origin.y; difference = difference < 0 ? difference : 0; NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ [self.scrolLView setContentOffset:CGPointMake(0, difference); } completion:^(BOOL finished) { }]; } 

And:

- (void)keyboardWillHide:(NSNotification *) notification { NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; [UIView animateWithDuration:duration animations:^{ [self.scrolLView setContentOffset:CGPointZero]; } completion:^(BOOL finished) { }]; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.