jrisberg
jrisberg

Reputation: 764

UIViews' frames being reset by scrolling containing UIScrollView

I have several UIViews contained in a vertically scrolling UIScrollView. I am using these views as makeshift pageviews, with behavior similar to that of a UIScrollView with paging enabled. The "pages" are flipped using a UIPageControl that calls a method (see below) to animate frame changes to flip back and forth.

PROBLEM: As soon as the UIScrollView that contains the UIViews is scrolled, the frames of the views are reset to their original values. When I page the views, the view.frame.origin.x value goes negative, to slide the views left off screen. As soon as I touch the scroll view to scroll, however, the x origin is reset to 0.

I am using the storyboard and therefore it's not easy to show the whole setup, but the page changing method can be seen here:

- (IBAction)changePage:(UIPageControl *)sender {
    UIView *view = [[UIView alloc] init];
    CGFloat width = self.view.frame.size.width;

    if (sender == self.infoPageControl) { view = self.infoView; }
    else if (sender == self.tempPageControl) {
        view = self.tempView;

        CGRect frame = CGRectMake((width - (width * (sender.currentPage+1))),
                                  self.tempHistoryContainerView.frame.origin.y,
                                  self.tempHistoryContainerView.frame.size.width,
                                  self.tempHistoryContainerView.frame.size.height);

        [UIView animateWithDuration:0.5 animations:^{
            [self.tempHistoryContainerView setFrame:frame];
        } completion:^(BOOL finished) {
            if (finished) {
                self.tempHistoryContainerView.frame = frame;
            }
        }];
    }

    CGRect frame2 = CGRectMake((width - (width * (sender.currentPage+1))),
                               view.frame.origin.y,
                               view.frame.size.width,
                               view.frame.size.height);

    [UIView animateWithDuration:0.5 animations:^{
        [view setFrame:frame2];
    } completion:^(BOOL finished) {
        if (finished) {
            view.frame = frame2;
        }
    }];
}

Upvotes: 3

Views: 977

Answers (1)

Jingshao Chen
Jingshao Chen

Reputation: 3495

The view probably is reset by autolayout constraints. Try to set this:

 self.tmpview.translatesAutoresizingMaskIntoConstraints=YES

This translates the frame set by your code into autolayout constraints so it won't pull your tmpview back.

Upvotes: 4

Related Questions