计算scrollview的内容大小 [英] Calculate the contentsize of scrollview

查看:133
本文介绍了计算scrollview的内容大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个scrollview作为tableview单元格的详细视图。我在通过界面构建​​器创建的详细视图有多个视图,如标签,按钮等。我通过界面构建​​器创建的是静态的。我把所有东西放在480高度的视图上。

I'm having a scrollview as the detailedview of tableview cell. There are multiple views on the detailedview like labels, buttons etc. which I'm creating through interface builder. What I'm creating through interface builder is static. I'm putting everything on a view of height 480.

我的详细视图上的标签是动态文本,可以扩展到任何长度。问题是我需要设置我需要其高度的scrollview的内容大小。

A label on my detailedview is having dynamic text which can extend to any length. The problem is that I need to set the scrollview's content size for which I need its height.

如果内容是动态的,我如何设置scrollview的高度?

How shall I set scrollview's height provided the content is dynamic?

推荐答案

您可以尝试使用scrollview'ers ContentSize。它适用于我,我使用动态内容的控件也有同样的问题。

You could try to use the scrollview'ers ContentSize. It worked for me and I had the same problem with the control using dynamic content.

    // Calculate scroll view size
float sizeOfContent = 0;
int i;
for (i = 0; i < [myScrollView.subviews count]; i++) {
    UIView *view =[myScrollView.subviews objectAtIndex:i];
    sizeOfContent += view.frame.size.height;
}

// Set content size for scroll view
myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width, sizeOfContent);

我在控制器中为持有scrollview的视图中的viewWillAppear方法执行此操作。这是我在super上调用viewDidLoad之前做的最后一件事。

I do this in the method called viewWillAppear in the controller for the view that holds the scrollview. It is the last thing i do before calling the viewDidLoad on the super.

希望它能解决你的问题。

Hope it will solve your problem.

// hannes

//hannes

这篇关于计算scrollview的内容大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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