Resizing a UIScrollView with an animation block moves the beginning

I scroll through the UIScrollView until the keyboard hides the UITextField. I reduce the height of the UIScrollView if it is closed, which works fine. but when I try to grow a UIScrollView heigh (back to the original size), the whole UIScrollView moves up and then animates to its original size and location. the origin increases by X and moves down to where it should be instead of the height of the drop-down view.

- (void)keyboardWillShow:(NSNotification *)n
{
    keyboardMove = self.rightScrollView.frame;
    offsetMove = self.rightScrollView.contentOffset;

    NSDictionary* userInfo = [n userInfo];

    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    [UIView animateWithDuration:.25
                          delay:0
                        options:(UIViewAnimationOptionAllowUserInteraction)
                     animations:^
                     {
                         self.rightScrollView.frame = CGRectMake(self.rightScrollView.frame.origin.x, self.rightScrollView.frame.origin.y, self.rightScrollView.frame.size.width, (self.view.frame.size.height - (self.rightScrollView.frame.origin.y + keyboardSize.height)));
                     }
                     completion:nil];

    if ((activeTextField.frame.origin.y + activeTextField.frame.size.height) > self.rightScrollView.frame.size.height)
    {        
        float contentOffsetMove = (self.rightScrollView.contentOffset.y + (activeTextField.frame.origin.y - self.rightScrollView.frame.size.height) + activeTextField.frame.size.height + 10);

        self.rightScrollView.contentOffset = CGPointMake(self.rightScrollView.contentOffset.x, contentOffsetMove);
    }
}

- (void)keyboardWillHide:(NSNotification *)n
{
    if ((activeTextField.frame.origin.y + activeTextField.frame.size.height) > self.rightScrollView.frame.size.height)
    {
        [UIView animateWithDuration:.25
                              delay:0
                            options:(UIViewAnimationOptionAllowUserInteraction)
                         animations:^{
                             [self.rightScrollView setContentOffset:offsetMove animated:YES];

                             self.rightScrollView.frame = keyboardMove;
                         }
                         completion:nil];

    }
    else
    {
        [UIView animateWithDuration:.25
                              delay:0
                            options:(UIViewAnimationOptionAllowUserInteraction)
                         animations:^{
                             self.rightScrollView.frame = keyboardMove;
                         }
                         completion:nil];
    }
}

therefore, when the keyboard is hiding, self.rightScrollView.origin moves up the X points and then animates at that location. I need the source to stay in place and the height to be larger (growing down).

any ideas why he is behaving with a spirit?

+5
3

, , , , iOS 6 - .

, UIScrollView , , UIScrollView . UIScrollView DOWN , UIScrollView 0. iPhone 4 (3.5 "), iPhone 5 (4" ) , , , Apple , UIKit.

, :

- (void)keyboardWillHide:(NSNotification *)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    if (self.scrollView.contentOffset.y > 0) {
        CGRect f = CGRectMake(0, keyboardSize.height / 2, self.scrollView.frame.size.width, self.scrollView.frame.size.height);
        self.scrollView.frame = f;
    }

    [UIView animateWithDuration:0.25 animations:^{
        self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}
+3

, UIKit, scrollView contentInset . :

self.rightScrollView.frame = CGRectMake(self.rightScrollView.frame.origin.x,
                                        self.rightScrollView.frame.origin.y,
                                        self.rightScrollView.frame.size.width,
                                        (self.view.frame.size.height - (self.rightScrollView.frame.origin.y + keyboardSize.height)));

self.rightScrollView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);
+2

Did you try to describe the animation exactly the way you want it to happen? That is, if you want the height to increase, do just that. Instead

self.rightScrollView.frame = keyboardMove; 

you could do

CGRect f = self.rightScrollView.frame;
f.size.height += keyboardMove.size.height - f.size.height;
self.rightScrollView.frame = f; 
0
source

All Articles