我有一个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,它似乎在加载viewcontroller几次后随机出现。
答案 0 :(得分:2)
这是因为scrollview在这个视图中还有2个东西,一个是水平滚动条,第二个是垂直滚动条,当你向上和向下滚动时左右自动显示