1

I'm working on an app that is in landscape view. I'm using a few UITextFields that when double tapped it will give you the ability to edit the TextFields. My question is how do I get the screen to scroll so the user can edit entire screen while the keypad is shown?

TWcode
  • 813
  • 2
  • 13
  • 27
  • please go through the below post http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present – DShah Jul 25 '11 at 13:21

3 Answers3

0

you can use uitextfield delegate method.

`- (void)textFieldDidBeginEditing:(UITextField *)textField
{
            [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.view.center=CGPointMake(self.view.center.x, self.view.center.y+60);
    [UIView commitAnimations];

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
            [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.view.center=CGPointMake(self.view.center.x, self.view.center.y-60);
    [UIView commitAnimations];

} `

Arun
  • 2,271
  • 1
  • 14
  • 18
0

You can just refer the solution at this link. Basically you need to move your scrollview by amounts so that your text field is visible. In yout textfieldbeginediting call the method below.

- (void)scrollViewToCenterOfScreen:(UIView *)theView {
    CGFloat viewCenterY = theView.center.y;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    CGRect keyboardBounds = CGRectMake(0, 280, 320, 200);
    CGFloat availableHeight = applicationFrame.size.height - keyboardBounds.size.height;    // Remove area covered by keyboard

    CGFloat y = viewCenterY - availableHeight / 2.0;
    if (y < 0) {
        y = 0;
    }
    scrollview.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
    [scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
Community
  • 1
  • 1
Praveen S
  • 10,355
  • 2
  • 43
  • 69
0

Using contentInset and scrollRectToVisible has served me well. The code below insets the scroll view so it is not covered by the keyboard, and then scrolls the content to display the the text field.

- (void)keyboardWillShow:(NSNotification *)aNotification 
{
    CGRect kbFrame;
    [[aNotification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&kbFrame];                    
    float kbHeight = [self convertRect:kbFrame fromView:nil].size.height;               
    float d = kbHeight - self.frame.origin.y / self.transform.a;        
    d = d < 0 ? 0 : d;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, d, 0.0);

    self.contentInset = contentInsets;
    self.scrollIndicatorInsets = contentInsets;

    UIView *responder = /* ... your text field ... */
    [self scrollRectToVisible:responder.frame animated:YES];        
    [self performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:0.0];
}

- (void)keyboardWillHide:(NSNotification *)aNotification 
{
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;

    [[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration 
                          delay:0 
                        options:animationCurve
                     animations:^{
                         self.contentInset = UIEdgeInsetsZero;
                         self.scrollIndicatorInsets = UIEdgeInsetsZero;                      
                     } 
                     completion:nil];

    [self setContentOffset:CGPointMake(0, 0) animated:YES]; 
    self.scrollEnabled = NO;
}
Martin Wickman
  • 19,662
  • 12
  • 82
  • 106
  • UIView *responder = ? tex2... Could you give an example putting the textField? – TWcode Jul 26 '11 at 00:35
  • Just do `UIView *responder = theTextField`, where `theTextField` is your text field. The reason is on the next line which fetches the size (frame) of the view. – Martin Wickman Jul 26 '11 at 10:46