我正在尝试在 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.
View Controller A 是滚动视图,其中包含容器视图,以及下面的更多内容.
View Controller A is the scrollview, which has the container view included, along with more content below.
视图控制器 B 是视图控制器,我希望它具有动态大小,并且所有内容都在视图控制器 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 ,
如果视图控制器 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.
我不确定视图控制器 B 是否有任何设置,我可以设置它以根据其内容自动更新其大小,或者我是否错过了任何其他技巧.任何帮助将不胜感激!
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!
有没有什么办法可以在不设置高度限制的情况下,根据 View Controller B 的大小来调整 A 中的 Container View 的大小?
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 会抱怨 - 有一个很好的理由:
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.
这篇关于在滚动视图中使用动态大小的控制器调整容器视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!