1

I am trying to make it so that when you click the email field and the keyboard pops up it would move the view up. But right now with this code it moves the view up no matter what textfield I click. Also I cant get the keyboard to dismiss. Not sure how to set this code to only scroll to the active field?

code:

- (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } // Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it's visible // Your application might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, self.emailField.frame.origin) ) { CGPoint scrollPoint = CGPointMake(0.0, self.emailField.frame.origin.y-kbSize.height); [scrollView setContentOffset:scrollPoint animated:YES]; } } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; } 

I have a view that looks like this (it is on a scrollview)

view

4
  • 2
    Take a look at github.com/michaeltyson/TPKeyboardAvoiding. It makes what you are trying to do really simple Commented May 17, 2013 at 14:21
  • You can check my SO answer stackoverflow.com/a/16481347/2315974 for a similar problem. Commented May 17, 2013 at 14:23
  • If you want to avoid writing the code for this yourself, there are some drop-in open source classes that do all of this automagically, like: github.com/michaeltyson/TPKeyboardAvoiding Commented May 17, 2013 at 14:35
  • @savner I tried this and this works very well actually! I think the answer from Thomas is also great/simple and works as well. Commented May 17, 2013 at 14:42

2 Answers 2

3

For this you can ignore the keyboard show/hide notifications and just use the UITextFieldDelegate protocol:

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if([textField isEqual:self.emailTextField]){ // scroll up } return true; } – (BOOL)textFieldShouldEndEditing:(UITextField *)textField { if([textField isEqual:self.emailTextField]){ // scroll back to start } return true; } 
Sign up to request clarification or add additional context in comments.

Comments

0
– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { if([textField isEqual:YourTextfieldName]) { } return true; // return true is needed. } – (BOOL)textFieldShouldEndEditing:(UITextField *)textField { if([textField isEqual:YourTextfieldName]) { } return true; //return true is needed. } 

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.