1

我有一个UIScrollview包含默认高度为 1717px 的数据,在运行时我必须添加一个或多个子视图,每个子视图的高度均为 540px。事先不知道需要添加多少。

我将这些添加到viewdidload方法中的滚动视图中,然后将其设置contentsize为正确的值。我还将 contentsize 设置为我需要的任何值。如果我在方法结束时记录它一切都很好并且contentsize仍然正确。

一旦显示它contentsize已恢复到 1717 像素,但内容仍然存在。如果滚动超出 的正常范围,我可以查看添加的内容一秒钟uiscrollview,但它会反弹回来。

我的相关部分viewdidload如下

NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"counter" ascending:YES]];
for (Facade * f in [self.inspection.facades sortedArrayUsingDescriptors:sortDescriptors]) {
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"facadeView" owner:self options:nil];
    facadeView *facadeInfo = [subviewArray objectAtIndex:0];
    facadeInfo.facade = f;
    facadeInfo.frame = CGRectMake(0, self.scrollView.frame.size.height, facadeInfo.frame.size.width, facadeInfo.frame.size.height);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height+facadeInfo.frame.size.height);
    self.scrollView.frame =  CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height+facadeInfo.frame.size.height);
    [self.scrollView addSubview:facadeInfo];
}
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);;
// now add our scroll view to the main view
[self.view addSubview:self.scrollView];

我能做些什么来阻止我contentsize重置为原始值?

4

3 回答 3

1

试试这个代码viewDidAppear而不是viewDidLoad.

于 2013-12-17T10:15:54.127 回答
0

I think

self.scrollView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height+facadeInfo.frame.size.height);

inside the for loop is the problem. you don't need to increase the frame size. if you do so you cant scroll. contentsize must be smaller then frame size. if not so scroll view can't be scroll.

于 2013-02-27T18:35:51.703 回答
-1

这是自动布局的问题。禁用自动布局,它将正常工作!!!:-)

于 2014-08-26T06:13:54.363 回答