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

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

问题描述

我正在尝试呈现有关分组为部分的对象的信息.它可能很长,所以每个部分都使用一个视图在视觉上分隔区域,并且都在滚动视图中.

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. 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天全站免登陆