[英]iOS: Best way to make a UIScrollView with dynamic height?
I'm currently setting up a UIScrollView with the following structure 我目前正在使用以下结构设置UIScrollView
UIScrollView
--ContentView (UIView)
--ContainerView1 (UIView)
--UILabel1
--UILabel2
--ContainerView2 (UIView)
--UILabel3
--UILabel4
--ContainerView3 (UIView)
--UILabel5
--UILabel6
I've pinned all the four edges for all the elements above and also have defined their height constraints (Storyboard complaints of zero height if I didn't do so). 我已经固定了上面所有元素的所有四个边缘,并且还定义了它们的高度限制(如果我不这样做,Storyboard会抱怨高度为零)。
Now whenever my UILabel receives its text from the server, I call sizeToFit for all the labels. 现在,每当我的UILabel从服务器接收文本时,我都会为所有标签调用sizeToFit。 Then I use the lastObject of the containerViews to calculate the supposing new height.
然后,我使用containerViews的lastObject计算假定的新高度。 Then I call setNeedsLayout.
然后我调用setNeedsLayout。 However, nothing changes in my app!
但是,我的应用程序没有任何变化!
This is the code that I use to calculate the new height and adjust. 这是我用来计算新高度并进行调整的代码。 Perhaps have I programmed it wrong?
也许我编程错了吗? Sorry my concept on constraints is not strong, may have set the constraints the wrong way too?
抱歉,我对约束的概念并不强,可能也以错误的方式设置了约束?
- (void) relayoutAllSubViews: (NSArray *) arrayOfContainerViews
{
//Relayout subviews
for (int i = 0; i < [arrayOfContainerViews count]; i++)
{
UIView * indView = [arrayOfContainerViews objectAtIndex:i];
if (indView.hidden == YES)
{
indView.frame = CGRectMake(indView.frame.origin.x, indView.frame.origin.y, indView.frame.size.width, 0);
}
else
{
UIView * lastSubviewInView = [indView.subviews lastObject];
CGFloat subviewEndPos = lastSubviewInView.frame.origin.y + lastSubviewInView.frame.size.height;
if ((subviewEndPos - (indView.frame.origin.y + indView.frame.size.height)) > 0)
{
indView.frame = CGRectMake(indView.frame.origin.x, indView.frame.origin.y, indView.frame.size.width, (subviewEndPos - indView.frame.origin.y));
}
}
}
//Adjust scrollview
UIView * lastView = [self.scrollView.subviews lastObject];
CGFloat newEndPos = lastView.frame.origin.y + lastView.frame.size.height;
self.contentView.bounds = CGRectMake(self.contentView.bounds.origin.x, self.contentView.bounds.origin.y, self.contentView.bounds.size.width, (newEndPos - self.contentView.bounds.origin.y));
self.scrollView.contentSize = self.contentView.bounds.size;
[self.view setNeedsLayout];
}
Need Help!! 需要帮忙!!
//use the below code to set the content size of a scroll view. //使用下面的代码设置滚动视图的内容大小。
float newHeight=0.0f;
for(UIView *subViews in [scrollViewObj subviews])
{
if([subViews isKindOfClass:[UIView class]])
{
newHeight=newHeight+(subViews.frame.origin.x+subViews.frame.size.height);
}
}
NSLog(@"%f",newHeight);
[scrollView setContentSize:CGSizeMake(300, newHeight)];//change the width as you need
//i have added code for dynamically adding the subview heights //我添加了用于动态添加子视图高度的代码
float newHeight=0.0f;
for(UIView *subViews in [scrollViewObj subviews])
{
if([subViews isKindOfClass:[UIView class]])
{
if([[subViews subviews] count]>0)
{
float heightOfSubviews=0.0f;
for(UIView *insideSubViews in [subViews subviews])
{
heightOfSubviews=heightOfSubviews+(insideSubViews.frame.origin.y+insideSubViews.frame.size.height);
}
NSLog(@"%f",heightOfSubviews);
[subViews setFrame:CGRectMake(subViews.frame.origin.x, subViews.frame.origin.y, subViews.frame.size.width, heightOfSubviews)];
}
}
}
UIView *viewLastObj=scrollViewObj.subviews.lastObject;
newHeight=viewLastObj.frame.origin.y+viewLastObj.frame.size.height;
NSLog(@"%f",newHeight);
[scrollViewObj setContentSize:CGSizeMake(300, newHeight)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.