问题描述
我有一个滚动视图作为表格视图单元格的详细视图.我正在通过界面生成器创建的详细视图上有多个视图,例如标签、按钮等.我通过界面生成器创建的是静态的.我把所有东西都放在高度 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.
我的详细视图上的标签具有可以扩展到任意长度的动态文本.问题是我需要设置滚动视图的内容大小,我需要它的高度.
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.
如果内容是动态的,我应该如何设置滚动视图的高度?
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);
我在控制器中名为 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.
//汉内斯
这篇关于计算scrollview的contentsize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!