views:

4696

answers:

4

I have an app where, in Interface Builder, I set up a UIView that has a text field near the bottom of the view. When I run the app and try to enter text into that field, the keyboard slides up overtop of the field so I can't see what I'm typing until I hide the keyboard again.

Has anyone else run into this problem and found a good way to solve it without either making the parent view scrollable or moving the text field farther up the screen?

+1  A: 

I guess one way would be to move your whole views position from (x,y) to (x,y-keybaardHeight) when the textfield is clicked and put it back when the keyboard is dismissed , might look a little odd as the view just comes up (maybe it wouldnt be bad if you animate it).

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame=self.view.frame;
    frame.origin=CGPointMake(x...//set point here
    self.view.frame=frame;
}
Daniel
+10  A: 

The usual solution is to slide the field (and everything above it) up with an animation, and then back down when you are done. You may need to put the text field and some of the other items into another view and slide the view as a unit. (I call these things "plates" as in "tectonic plates", but that's just me). But here is the general idea if you don't need to get fancy.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self animateTextField: textField up: NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    const int movementDistance = 80; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? -movementDistance : movementDistance);

    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}
Amagrammer
+4  A: 

This worked wonders for me http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html

In particular it has the benefit of calculating the slide animation distance depending on the position of the text field.

cidered
+1  A: 

There's a great walkthrough at http://acts-as-geek.blogspot.com/2009/11/editing-textfields-without-obscuring.html. It shows how to move an existing UIView onto a UIScrollView, and to scroll it automatically when the keyboard appears.

I've updated it a bit to calculate the correct height for the UIScrollView when there are controls (such as a UITabBar) below the UIScrollBar. See gargantuchet.blogspot.com/2009/12/i-came-across-post-updating-uiview-to.html (sorry that's not formatted as a URL; since I'm a new user, stackoverflow is limiting me to one URL per post).

GargantuChet