1

这是我的困境。我在 UIScrollView 中有 4 个元素。1. 最上面的元素是一个 UILabel,我根据其中的内容量动态给出高度。2. 第二个是固定高度的 UILabel,我根据给定上部 UILabel 3 的高度动态给出位置。第三个元素是 UIImageView,我必须再次根据给定最高 UILabel 4 的高度动态给出位置。第四个是一个 UIWebView,我动态地给出了高度和位置。(高度取决于其中的内容.. 并再次根据最上面的 UILabel 的高度定位)

最后,我动态地为我的 UIScrollView 赋予高度以容纳上述所有元素。

这是我在 - (void)webViewDidFinishLoad:(UIWebView *)webView 中使用的代码来完成上述所有操作。

//Adjust height of top-most UILabel
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize expectedLabelSize = [item.label1 sizeWithFont:label1.font constrainedToSize:maximumLabelSize lineBreakMode:label1.lineBreakMode];
CGRect newFrame = label1.frame;
newFrame.size.height = 0;
newFrame.size.height = expectedLabelSize.height;
label1.frame = newFrame;

//Adjust position of second UILlabel
CGRect labelPosition = label2.frame;
labelPosition.size.height = 20;
labelPosition.origin.y = expectedLabelSize.height +14;
label2.frame = labelPosition;

//Add UIImageView and adjust it's position
UIImageView *image;
image = [[UIImageView alloc] initWithFrame:CGRectMake(0, expectedLabelSize.height +41, 320, 2)];
image.image = [UIImage imageNamed:@"image.png"];
[scrollView addSubview:image];
[image release];

//Adjust UIWebView height and position
CGRect frame = webView.frame;
frame.size.height = 0;
frame.origin.y = expectedLabelSize.height +48;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;

//Adjust Scrollview height
scrollView.contentSize = CGSizeMake(320, fittingSize.height +expectedLabelSize.height +48);

最后,我的问题是当我第一次加载这个视图时,除了滚动视图之外的所有东西都得到了正确的高度和位置。但是,如果我返回一个视图并再次打开此视图,则滚动视图具有所需的高度。

有什么想法我可能在这里做错了吗?

4

1 回答 1

0

我的猜测是第一次运行时 scrollView 变量尚未初始化。尝试在此代码中的某处设置断点并检查 scrollView 是否有值或者它是否只是 0x00000000。

于 2012-07-21T17:20:31.183 回答