2013-02-27 89 views
1

我有一个UIScrollview包含默认高度为1717px的数据,在运行时我必须添加一个或多个子视图,每个子视图540px高。事先未知其中有多少将不得不被添加。UIScrollview重置内容

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

一旦显示contentsize已经恢复到1717px,内容仍然存在。如果我滚动到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

回答

0

我觉得

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

里面的for循环的问题。你不需要增加帧的大小。如果你这样做,你不能滚动。 contentize必须小于frame size。如果不是这样,则滚动视图不能滚动。

+0

滚动视图的大小是好的,在循环后它会变回正确的值。它主要用于跟踪内容必须在循环的下一次迭代中的大小。我可以在那里更改代码,以便不使用框架大小来计算我的内容大小,但是当我编写代码时没有看到选项。 – Arperum 2013-02-27 19:13:27

1

请尝试在viewDidAppear而不是viewDidLoad此代码。

-1

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