具有自动布局的UIScrollView内具有动态高度的容器视图 [英] Container view with dynamic height inside UIScrollView with auto layout

查看:192
本文介绍了具有自动布局的UIScrollView内具有动态高度的容器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIScrollView,它嵌套在一个内容视图中,并且有两个子视图嵌套,一个具有已知高度的常规UIView,以及一个根据内容而具有动态高度的容器视图.像这样:

I have a UIScrollView, nested inside of which in one content view and it has two child views nested, a regular UIView with a known height, and a container view with dynamic height depending on is content. Like so:

视图如下:

我的约束设置如下:

滚动视图"仅限于其超级视图(即视图)的尾部,前缘,顶部和底部边缘

The Scroll View is constrained to the trailing, leading, top and bottom edges of its superview (i.e. the View)

内容视图"限于其超级视图的尾部,前缘,顶部和底部边缘(即滚动视图") 它的宽度约束也等于主视图(即视图),因此滚动视图的宽度与屏幕宽度相同.

The Content View is constrained to the trailing, leading, top and bottom edges of its superview (i.e. the Scroll View) It also has a width constraint equal to the main view (i.e. the View) so the width of the scrollview is the same as the screen width.

顶视图限于其超级视图(即内容视图)的前缘,后缘和上边缘

The top view is constrained to the leading, trailing and top edges of its superview (i.e. the Content View)

Container View限于其超级视图(内容视图)的尾部,前缘和底部边缘 它的顶部边缘也被限制在顶视图"的底部边缘.

The Container View is constrained to the trailing, leading and bottom edges of its superview (the Content View) Its top edge is also constrained to the bottom edge of the Top View.

容器视图的视图层次结构如下:

The view hierarchy of the Container View looks like this:

左上角的标签被限制在其superview的尾部,前部和顶部边缘. 右下角的标签被限制在其视图的尾部,前部和底部边缘. 顶部标签对底部标签具有垂直约束.为了测试的目的,我已经将此垂直约束变得更大(1000点).

The top left label is constrained to the trailing, leading and top edges of its superview. The bottom right label is constrained to the trailing, leading and bottom edges of its superview. The top label has a vertical constraint to the bottom label. I've made this vertical constraint extra large for the purpose of my tests (1000 points).

这应该使内容视图的高度约为1000点.

This should give the content view a height of ~1000 points.

我的理解是现在可以解决容器视图的高度问题,即内容视图将调整为顶视图的高度+容器视图的高度.

My understanding was with a height now resolved for the container view, that the Content View would resize to the height of the Top View + the height of the Container View.

但是IB抱怨以下问题:

But IB complains with the following:

它想要调整Container View的大小并将其高度设置为0.如果我给Container View一个明确的高度,那么一切都会按预期进行,但这不是我想要的,因为Container View可以根据其动态变化内容大小.

It wants to resize the Container View and give it a height of 0. If I give the Container View an explicit height then everything works as expected but this is not what I need want since the Container View can dynamically change depending on its content size.

感谢您的帮助!

推荐答案

我发现有两件事可以帮助我解决同一问题.

There were two things I found that helped me work around this same issue.

我希望容器视图自行调整大小,但是IB希望在容器视图上具有明确的高度.由于IB对视图的内容一无所知,因此它无法知道容器视图的内容可以调整自身大小.最简单的方法是在Size Inspector中为容器视图设置Placeholder Intrinsic Content Size:

I want the container view to resize itself, but IB wants an explicit height on the container view. Since IB doesn't know anything about the content of the view, it has no way of knowing the content of the container view can size itself. The simplest way to do that is to set a Placeholder Intrinsic Content Size from the Size Inspector for the container view:

这有效地导致不施加任何高度限制而使IB开心.另一种选择是在容器上添加在构建时删除"高度限制.

This effectively results in making IB happy without applying any height constraint. Another option is to add a "Remove at build time" height constraint on the container.

子视图控制器的根视图默认使用AutoresizingMask,这是任何UIViewController中最顶层视图的标准配置.我的解决方案是在添加子视图控制器时,禁用prepareForSegue中的AutoresizingMask.在父级视图控制器中尝试以下操作:

The root view of the child view controller defaults to using an AutoresizingMask, as is standard for the topmost view in any UIViewController. My solution to this is to disable use of AutoresizingMask in prepareForSegue, when the child view controller is added. Try the following in the parent view controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    if let childViewController = segue.destination as? ChildViewControllerClass {
        childViewController.view.translatesAutoresizingMaskIntoConstraints = false
    }
}

在父视图中进行更改可确保如果子视图在UINavigationController中的其他位置重复使用,则视图的大小将正确.

Making the change in the parent view ensures that if the child view is re-used elsewhere inside a UINavigationController, the view will be sized correctly.

在进行此更改之前,我不断遇到与称为UIView-Encapsulated-Layout-Height的约束冲突的AutoLayout错误,我相信这是从根UIViewController视图上基于AutoresizingMask的布局派生的约束的名称.

Before I made that change, I kept getting AutoLayout errors conflicting with constraints called UIView-Encapsulated-Layout-Height, which I believe is the name for constraints derived from AutoresizingMask based layout on the root UIViewController view.

这篇关于具有自动布局的UIScrollView内具有动态高度的容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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