UIViews frames are reset scrolling containing UIScrollView

I have multiple UIViews contained within a vertical scrolling UIScrollView. I am using these views as temporary page views, with behavior similar to that of a paging enabled UIScrollView. "Pages" are flipped using a UIPageControl, which calls a method (see below) to change the frame to flip back and forth.

PROBLEM: As soon as the UIScrollView containing the UIViews is scrolled, the view frames are reset to their original values. When I view the views, the view.frame.origin.x value becomes negative to shift the views left on the screen. However, as soon as I touch the scroll to scroll, the origin of x is reset to 0.

I am using a storyboard and so it is not easy to show the entire setup, but the way to change the page 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;
        }
    }];
}

      

+3


source to share


1 answer


This is probably the expression reset by autodetection constraints. Try to install this:

 self.tmpview.translatesAutoresizingMaskIntoConstraints=YES

      



This translates the frame given by your code into autodetection constraints, so it won't pull yours tmpview

back.

+4


source







All Articles