自动版式一个UIScrollView以适应内容,包括子视图和分组表 [英] Autolayout a UIScrollView to fit content including subviews and grouped tables

查看:134
本文介绍了自动版式一个UIScrollView以适应内容,包括子视图和分组表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想大概分成部分的对象present信息。这可能是很长,所以每部分使用,以便在视觉上分隔区域,它是所有在滚动视图。

I am trying to present information about an object grouped into sections. It may be long, so each section uses a view to visually separate the areas and it is all in a scroll view.

第一子视图有一个文本字段,它调整大小以适应文本,与其父视图还调整大小以适合文本字段。

The first subview has a text field that does resize to fit the text, and its parent view also resizes to fit the text field.

第二子视图有一个分组表显示数据。我希望所有在表中的数据的出现,应该没有表滚动。

The second subview has a grouped table to display data. I want all of the data in the table to appear and there should be no table scrolling.

第三子视图有更多的信息,但其内容是这个演示的目的,一个空的观点。在真正的应用程序我可能添加自定义子视图的任意数量的。

The third subview has more information but its contents are an empty view for the purposes of this demo. In the real app I may add an arbitrary number of custom subviews.

什么我发现是,第一子视图调整大小合适,但我无法找到的布局约束相结合,这将使第二子视图大小以适合表和向下移动第三种观点。

What I'm finding is that the first subview resizes appropriately but I cannot find the combination of layout constraints that will make the second subview size to fit the table and move the third view down.

什么我需要做的,使滚动视图适合其内容,当子视图的内容可能会调整?表视图填充父视图的可用空间,但父视图并不扩展到适合。我注意到,第三种观点具有顶部空间限制到上海华,不只是它上面的观点。这是在下面

What do I need to do to make the scroll view fit its contents, when the sub view's contents may resize? The table view is filling the available space in the parent view, but the parent view is not expanding to fit. I note that the third view has a top space constraint to the superview, not just to the view above it. That's visible in the screenshot below

推荐答案

我发现了一个解决方案,最喜欢的解决方案,回想起来显而易见的。

I found a solution that like most solutions, seems obvious in retrospect.

我想我的问题是,内部的tableview没有固定的高度和,因为它也是个滚动视图,试图以适合任何被分配的,而不是扩大以适应室内的内容。我能够通过添加IB固定高度约束,挂钩它在我的视图控制器的插座,解决我的问题。然后在控制器:

I think my problem is that the interior tableview had no fixed height and since it also a scrollview, was trying to fit whatever was assigned instead of expanding to fit the interior content. I was able to solve my problem by adding a fixed height constraint in IB and hooking it up to an outlet in my view controller. Then in the controller:

-(void) viewWillLayoutSubviews{
    [self.tableView layoutIfNeeded];
    self.tableViewHeightConstraint.constant = self.tableView.contentSize.height;
}

DERP。综上所述表视图需要一个高度,所以我设置之一。

Derp. In summary the table view needed a height so I set one.

下面是截图,请注意右侧的固定高度约束:

Here's a screenshot, note the fixed height constraint on the right hand side:

这篇关于自动版式一个UIScrollView以适应内容,包括子视图和分组表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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