dimanche 28 juin 2015

Content shows lower in UIScrollView when app first opened

I have my content in a UIView embedded in a UIScrollView showing lower than it should when I first open the view.

enter image description here

However, when I then click on any of the textfields, it scrolls up...

enter image description here

And returns exactly to the place where it is supposed to be

enter image description here

This is the code I am using:

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

    self.info = [aNotification userInfo];
    CGSize kbSize = [[self.info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.scrollView.frame;

    aRect.size.height -= kbSize.height;
    CGPoint origin = self.activeField.frame.origin;
    origin.y -= self.scrollView.contentOffset.y;
    //origin.y += activeField.frame.size.height;

    if (!CGRectContainsPoint(aRect, origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeField.frame.origin.y + self.activeField.frame.size.height - aRect.size.height);
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }

}

Works fine for iPad (landscape) but not for iPhone (portrait). Any ideas?

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire