UIViews'通过滚动包含UIScrollView重置帧

时间:2014-08-17 17:11:59

标签: ios uiview uiscrollview

我在垂直滚动的UIScrollView中包含了几个UIView。我使用这些视图作为临时浏览量,其行为类似于启用了分页的UIScrollView。使用调用方法(见下文)的UIPageControl翻转“页面”以动画帧更改以来回翻转。

问题:只要滚动包含UIViews的UIScrollView,视图的帧就会重置为原始值。当我查看视图时,view.frame.origin.x值变为负数,以滑动屏幕左侧的视图。但是,只要我触摸滚动视图进行滚动,x原点就会重置为0.

我正在使用故事板,因此显示整个设置并不容易,但可以在此处看到页面更改方法:

- (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;
        }
    }];
}

1 个答案:

答案 0 :(得分:4)

视图可能会被autolayout约束重置。尝试设置:

 self.tmpview.translatesAutoresizingMaskIntoConstraints=YES

这会将您的代码设置的框架转换为自动布局限制,因此不会将您的tmpview拉回来。