我如何prepare一个UIViewController的视图被添加到一个UIStackView? [英] How do I prepare a UIViewController's view for being added to a UIStackView?

查看:201
本文介绍了我如何prepare一个UIViewController的视图被添加到一个UIStackView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要做的...

This is what I'm trying to do...

我有一个需要基于一些数据的presence动态显示的不同子视图一个视图控制器。

I have one view controller that needs to dynamically display different subviews based on the presence of some data.

下面是一个简单的样机。 每个彩色块重presents独特的子视图

Here is a simple mockup. Each colored block represents a unique subview.

简单样机

有时绿色块需要在顶部,有时绿色块将不会显示在所有的,有时淡蓝色块将不同的东西,等等。

Sometimes the green block needs to be at the top, sometimes the green block won't display at all, sometimes the light blue block will be something different, etc.

每个子视图有互动元素,所以我一直在创建和加入他们,像这样:

Each subview has interactive elements, so I've been creating and adding them like so:


  1. 定义一个新的视图控制器

  2. 定义视图

  3. 电话 addChildViewController didMoveToParentViewController

  4. 电话 addSubview myNewViewController.view

  5. 使用SnapKit使自动布局约束来定位视图

我想,因为它似乎对这一观点很好的支持系统,因为所有我需要做的是堆子视图过渡到 UIStackView 。尝试与自己内心的汽车布局限制添加子视图时,我看到许多相互矛盾的约束错误和意外视图帧。

I want to transition to UIStackView because it seems a good support system for this view because all I need to do is stack its subviews. I'm seeing many conflicting constraint errors and unexpected view frames when trying to add subviews with their own inner auto layout constraints.

我是自己设置为失败在这里通过嵌入的4-6视图控制器的意见,在一个视图控制器的看法?

Am I setting myself up for failure here by embedding the views of 4-6 view controllers in the view of one view controller?

另外,我怎么给如最小高度或内容大小添加的视图的属性,没有看到与 UIStackView 破许多制约因素? (因此,它们可以堆叠,但它们中的一个表示,400高,而另一个是200高)

Also, how do I give the added views properties like minimum heights or content sizes without seeing many breaking constraints with UIStackView? (So they can stack, but one of them is say, 400 tall, and the other is 200 tall)

推荐答案

您完全可以做到这一点使用 UIContainerViews 结合 UIStackViews UIScrollViews ,这是一个复杂的设置,使这里有一个启动项目,让你开始:

Answer

You can absolutely do this using UIContainerViews combined with UIStackViews and UIScrollViews, it's a complicated setup so here's a starter project to get you started:

https://github.com/Rnorback/ScrollingStackView

下面是该项目的样子:

在这里输入的形象描述

您想要的容器有不同的大小。为了做到这一点,只需添加高度限制与自动布局的容器。如果你想改变滚动的高度。然后,你需要改变的高度约束的 UIStackView

You want the containers to have different sizes. In order to do that, simply add height constraints to the containers with Autolayout. If you want to change the height of the scrolling. Then you'll need to change the height constraint of the UIStackView.

在创建此设置,您必须确保在 UIStackView 分配在填充设置住宿。这样,你可以在设置UIContainerViews高度限制。

When creating this setup, you have to make sure the UIStackView distribution setting stays in fill. That way you can set height constraints on UIContainerViews.

当设置在任何一个的UIScrollView 你必须确保该对象被固定在滚动视图的边缘,有一个定义的宽度和高度,否则,滚动视图将抛出一个constriant错误。我认为它像滚动视图中试图preSS在您的内容视图的各个方面,如果任何一方让步,滚动视图将无法present正确。

When setting up anything in a UIScrollView you have to make sure that the object is pinned to the edges of the scroll view and has a defined width and height, otherwise the scrollview will throw a constriant error. I think of it like the scrollview tries to press in on all sides of your content view and if any side gives, the scrollview won't be able to present it properly.

希望这有助于。

这篇关于我如何prepare一个UIViewController的视图被添加到一个UIStackView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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