0

I am making an app with multiple UITextField and UITextView The UITextViewis in the bottom of the screen and whenever typing starts, the keyboard blocks the UITextView

How will I able to move up the view of the form when the keyboard appears on the screen? then move it down again when the keyboard disappears?

Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69
Chano Tarongoy
  • 155
  • 1
  • 2
  • 8
  • Yep, you have to scroll the screen up while the keyboard is displayed. – Hot Licks Apr 11 '13 at 02:20
  • 3
    And you really really couldn't find *any* answers to this *anywhere* on Stack Overflow or *anywhere*? – matt Apr 11 '13 at 02:20
  • http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – matt Apr 11 '13 at 02:21

3 Answers3

1

The best answer is to try and avoid doing that.

However, if you put your stuff in a UIScrollView or a UITableView you can scroll to the input when it becomes first responder.

Lee Irvine
  • 3,057
  • 1
  • 15
  • 12
0

I like to use this: https://github.com/michaeltyson/TPKeyboardAvoiding It works really well for me and is easy to use.

Lucas Goossen
  • 527
  • 7
  • 15
0

Make sure you implement the UITextFieldDelegate in your class. These delegate methods should do the trick for a text field named activeField:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [activeField setDelegate:self];    
    [self configureView];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return TRUE;
}

// 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, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.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;
}

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}
Devin Lynch
  • 289
  • 2
  • 14