我有一个 UIScrollView 我添加了几个子视图(完成loadView
)(见代码):
// Add the views to the Scroll View
[scrollView addSubview:label];
[scrollView addSubview:careerDescriptionWebView];
self.view = scrollView;
然后我稍后想要计算添加的子视图的高度(完成viewWillAppear:
以设置 UIScrollView 的高度(参见此代码):
// Change the height of the Scroll View dynamically to match the content
float hgt = 0;
for (UIView *view in scrollView.subviews) {
hgt += view.frame.size.height;
NSLog(@"View: %@, height: %f", [[view class] description], view.frame.size.height);
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,hgt)];
所以后来当我检查控制台的输出时,我看到了:
2011-06-23 13:34:48.344 Acando[90355:207] View: UILabel, height: 21.000000
2011-06-23 13:34:48.345 Acando[90355:207] View: UIWebView, height: 1017.000000
2011-06-23 13:34:48.346 Acando[90355:207] View: UIImageView, height: 7.000000
2011-06-23 13:34:48.347 Acando[90355:207] View: UIImageView, height: 145.000000
这不会立即出现,起初它只有一个 UILabel 和一个 UIWebView 正确显示,它似乎在加载视图控制器几次后随机出现。