在滚动视图内使用动态大小的控制器调整容器视图的大小 [英] Sizing a Container View with a controller of dynamic size inside a scrollview

查看:93
本文介绍了在滚动视图内使用动态大小的控制器调整容器视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UIScrollView内部具有动态高度的控制器创建容器视图,并使用自动布局自动调整大小。

I'm trying to create a container view, with a controller that has a dynamic height, inside a UIScrollView and have it sized automatically using auto layout.

< img src =https://i.stack.imgur.com/J7j7t.pngalt =说明设置的故事板>

视图控制器A是滚动视图,包含容器视图,以及下面的更多内容。

View Controller A is the scrollview, which has the container view included, along with more content below.

视图控制器B是我想要具有动态大小的视图控制器,并且所有内容都是在View Controller A的滚动视图中以全高显示。

View Controller B is the view controller that I want to have a dynamic size and for all the content to be displayed in full height in View Controller A's Scroll View.

我在获取动态大小B以自动设置A中容器视图的大小时遇到​​一些问题。但是,如果我在A ,

I'm having some problems getting the dynamic size of B to automatically set the size of the Container View in A. However if I set a height constraint on the Container View in A ,

如果View Controller B的高度也是250,那么这将是预期的输出。它也适用于高度1000,所以据我所知,所有自动布局约束都正确设置。不幸的是,由于高度实际上应该是动态的,我想避免设置高度约束。

It would be the expected output if View Controller B would also have 250 height. It also works fine for height 1000, so as far as I know, all the auto layout constraints are properly setup. Unfortunately, since the height should actually be dynamic, I would like to avoid setting a height constraint at all.

我不确定是否有任何设置视图控制器BI可以设置它根据其内容自动更新其大小,或者是否有任何其他技巧我错过了。任何帮助将不胜感激!

I'm not sure if there are any settings for view controller B I can set for it to automatically update its size depending on its contents, or if there are any other tricks I've missed. Any help would be much appreciated!

根据视图控制器B的大小没有设置高度约束,有没有办法在A中调整容器视图的大小?

Is there any way to size the Container View in A according to how big the size of View Controller B is without setting a height constraint?

推荐答案

是的,有。我设法在我自己的一个项目中实现了这种行为。

Yup, there is. I managed to achieve that kind of behavior in one of my own projects.

你要做的就是告诉系统它不应该添加模仿固定框架的约束在Interface Builder中设置根视图。执行此操作的最佳位置是在触发嵌入segue时在容器视图控制器中:

All you gotta do is to tell the system that it should not add constraints that mimic the fixed frame set for your root view in Interface Builder. The best place to do this is in your container view controller when your embed segue is triggered:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // You might want to check if this is your embed segue here
    // in case there are other segues triggered from this view controller. 
    segue.destinationViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
}



重要提示:



您必须确保加载到容器中的视图从上到下受到约束,并且您需要将其中一个垂直约束的优先级设置为低于1000的值。(总是使用底部是一个好习惯这是必要的。)这是必要的,因为否则Interface Builder会抱怨 - 有充分的理由:

Important:

You gotta make sure that the view that you load into the container is constrained from top to bottom and you need to set the priority of one of the vertical constraints to a value lower than 1000. (It's a good practice to always use the bottom constraint for this.) This is necessary because otherwise Interface Builder will complain — with a good reason:

在设计时你的根视图有一个固定的大小(高度)。现在,如果所有子视图都具有固定高度并且与具有相同优先级的固定约束连接,则除非固定根视图的高度恰好与子视图的总高度和垂直约束完全匹配,否则无法满足所有这些要求。如果将其中一个约束的优先级降低到999 Interface Builder知道哪个约束要中断。但是在运行时 - 当如上所述设置 translatesAutoresizingMaskIntoConstraints 属性时 - 根视图没有固定的框架,系统将使用你的999优先级约束。

At design time your root view has a fixed size (height). Now if all your subviews have a fixed height and are connected with fixed constraints that all have the same priority it's impossible to fulfil all these requirements unless the height of your fixed root view coincidentally matches exactly the total height of your subviews and the vertical constraints. If you lower the priority of one of the constraints to 999 Interface Builder knows which constraint to break. At runtime however — when the translatesAutoresizingMaskIntoConstraints property is set as stated above — there is no fixed frame for your root view anymore and the system will use your 999 priority constraint instead.

这篇关于在滚动视图内使用动态大小的控制器调整容器视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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