1
votes

So, as often suggested for people who want the view to be pushed up when the keyboard appears, I use a UIScrollView. I copied and pasted the information from this page -- http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7 -- to do that...

But what happens is, when I tap the text fields, the keyboard pops up, but the view actually scrolls DOWN, revealing black on top. After that, I can actually manually scroll the view up and reveal the hidden text fields...obviously, that's not what I'm trying to do!

Any ideas? I have the following in the viewDidLoad() method:- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

I've tried this version of keyboardWasShown:

- (void) keyboardWasShown:(NSNotification *)aNotification
{
    NSDictionary * info = [aNotification userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0,keyboardSize.height, 0);
    [[self introScroll] setContentInset:contentInsets];
    [[self introScroll] setScrollIndicatorInsets:contentInsets];

    CGRect ltdRect=[[self view] frame];
    ltdRect.size.height=keyboardSize.height;
    if (!CGRectContainsPoint(ltdRect,[self activeField].frame.origin))
    {
        CGPoint scrollPoint = CGPointMake(0,[self activeField].frame.origin.y-keyboardSize.height);
        [[self introScroll] setContentOffset:scrollPoint animated:YES];
    }
    NSLog(@"Keyboard out!");
}

as well as this version of keyboardWasShown:

- (void)keyboardWasShown:(NSNotification*)aNotification {

    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    CGRect bkgndRect = [[[self activeField] superview ]frame];
    bkgndRect.size.height += kbSize.height;

    [[[self activeField] superview] setFrame:bkgndRect];

    [[self introScroll] setContentOffset:CGPointMake(0.0, [self activeField].frame.origin.y-kbSize.height) animated:YES];

}

Both give me the exact same results.

What's going on?? I'm using Xcode 4.6.

1
My solution? AlexWien's "add 44 to scrollpoint.y" -- except I made it 344. :) NOW it works. :) Thanks, Alex... BTW, any way to make the upward scrolling in sync with the keyboard so they both move at the same time? - ScatteredFrog
My solution posted below does not work anymore for ios7. Have you solved it for ios7, too? How? - AlexWien

1 Answers

0
votes

I had a similar problem that my text fields did not scroll into view.
the apple code is not perfect, I had to modify it: Try to solve that by adding the height of active field in line:

frame.origin.y + frame.size.height - keyboardSize.height

If that does not work, add 44 to scrollpoint.y (size of navigation bar)

Update for ios7:

I improved unwanted and wrong scrolling in ios7 for text fields, that are already in view by

if (scrollPtY < 0) {
        return;
}