UIScrollView ContentSize.height 默认为 0.0000? [英] UIScrollView ContentSize.height is 0.0000 by default?

查看:20
本文介绍了UIScrollView ContentSize.height 默认为 0.0000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UIScrollView 创建动态表单,但是我注意到当我使用 NSLog 时,我的 scrollView 的 contentSize.height = 0.00000 我将 scrollView 构建为故事板上屏幕的宽度和高度,其中包含元素.那么为什么它返回0呢?scrollView 内的所有元素都正确显示/运行.scrollView 的 contentSize 是否总是默认为 0 而我只需要设置它?

I am trying to create a dynamic form using a UIScrollView, but I noticed when I used a NSLog that my scrollView's contentSize.height = 0.00000 I built the scrollView to the width and height of the screen on storyboard with elements inside it. So why does it return 0? All the elements inside the scrollView are showing/functioning correctly. Does scrollView's contentSize always default at 0 and I just need to set it?

推荐答案

Yes 默认为 0.您应该通过将所有视图的高度和它们之间的空间相加来设置滚动视图的内容大小,例如:

Yes is 0 by default. You should set the content size of your scrollview by summing all the views height and the spaces between them, example:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
scrollView.backgroundColor = [UIColor blueColor];
[self.view addSubview:scrollView];

NSUInteger spaceBetweenLabels = 10;

UILabel *firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 40)];
firstLabel.text = @"first label text";

UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, spaceBetweenLabels + firstLabel.frame.size.height, 300, 40)];
secondLabel.text = @"second label text";

[scrollView addSubview:firstLabel];
[scrollView addSubview:secondLabel];

CGSize fullContentSize = CGSizeMake(300, firstLabel.frame.size.height + secondLabel.frame.size.height + spaceBetweenLabels);

[scrollView setContentSize:fullContentSize];

当然,如果您的滚动视图高度高于内容大小高度,它就不会滚动,因为所有内容都在屏幕上.

Of course if your scroll view height is higher than the content size height it won't scroll because everything is on the screen.

这篇关于UIScrollView ContentSize.height 默认为 0.0000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆