2012-05-29 86 views
0

我有一个UIScrollView,其中有一个UIImageView和一个UILabel,它们都是sizeToFit。我如何在UIImageView和UILabel的高度基础上设置UIScrollView自动调整的高度?UIScrollView弹性高度

回答

0

您可以在界面构建器中设置这样的开关。你将不得不执行你的UITableView以下的委托方法:

- 的tableView:heightForRowAtIndexPath:

的方法

文档可以在这里找到:

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

在这种方法中,你将有访问相应行所包含元素的框架。在你的情况下,UILabel和UIImageView并计算它们的帧的组合高度。

希望这会有所帮助。

+0

嗯,其实这是UIScrollView的。 :-) – 2012-05-29 23:03:39

+0

阿尔我读错了,我的道歉。 –

0

不要在UIScrollView每个子视图循环,并获得每一个项目的高度,然后,将其设置为ScrollView,如:

int height = 0; 
for(UIView *subview in [yourScrollView subviews]) { 
    height += subview.frame.size.height; 
} 
yourScrollView.contentSize = CGRectMake(yourScrollView.frame.origin.x, yourScrollView.frame.origin.y, yourScrollView.frame.size.width, height); 

我不知道这会工作,但它是一个解。

希望得到这个帮助。

+0

这项工作假设UIImageView位于UILabel之上,反之亦然 - 如果它们彼此相邻,则循环查找最高的项目并将其用作高度。 :) – WendiKidd

+0

这就是我的想法。我会尽力找到一个解决方案。 :-) – 2012-05-29 23:55:31

0

您可以通过下面的代码

float height = 0.0; 
for(UIView *subview in [yourScrollView subviews]) { 
    if(height < (subview.frame.origin.y + subview.frame.size.height)) 
     height = subview.frame.origin.y + subview.frame.size.height; 
} 
yourScrollView.contentSize = CGRectMake(yourScrollView.frame.origin.x, yourScrollView.frame.origin.y, yourScrollView.frame.size.width, height); 

希望这将帮助您找到滚动视图的高度!

0

yourLabel.frame = CGRectMake(0, 200, yourImageView.height, yourLabel.frame.size.height); 
float heightOfScrollView = yourLabel.frame.origin.y + yourLabel.frame.size.height + 10; 
scrView.contentSize = CGSizeMake(320, heightOfScrollView); 

希望这有助于你....