問題描述
我正在嘗試在 UIScrollView 內(nèi)創(chuàng)建一個(gè)具有動(dòng)態(tài)高度的控制器的容器視圖,并使用自動(dòng)布局自動(dòng)調(diào)整其大小.
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 是滾動(dòng)視圖,其中包含容器視圖,以及下面的更多內(nèi)容.
View Controller A is the scrollview, which has the container view included, along with more content below.
視圖控制器 B 是視圖控制器,我希望它具有動(dòng)態(tài)大小,并且所有內(nèi)容都在視圖控制器 A 的滾動(dòng)視圖中以全高顯示.
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 的動(dòng)態(tài)大小以自動(dòng)設(shè)置 A 中的容器視圖的大小時(shí)遇到??一些問題.但是,如果我在 A 中的容器視圖上設(shè)置高度限制 ,
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,這將是預(yù)期的輸出.它也適用于高度 1000,據(jù)我所知,所有自動(dòng)布局約束都已正確設(shè)置.不幸的是,由于高度實(shí)際上應(yīng)該是動(dòng)態(tài)的,所以我想完全避免設(shè)置高度約束.
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 是否有任何設(shè)置,我可以設(shè)置它以根據(jù)其內(nèi)容自動(dòng)更新其大小,或者我是否錯(cuò)過了任何其他技巧.任何幫助將不勝感激!
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!
有沒有什么辦法可以在不設(shè)置高度限制的情況下,根據(jù) View Controller B 的大小來調(diào)整 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?
推薦答案
是的,有.我設(shè)法在自己的一個(gè)項(xiàng)目中實(shí)現(xiàn)了這種行為.
Yup, there is. I managed to achieve that kind of behavior in one of my own projects.
您所要做的就是告訴系統(tǒng)它不應(yīng)該添加模仿在 Interface Builder 中為您的根視圖設(shè)置的固定框架的約束.當(dāng)您的嵌入 segue 被觸發(fā)時(shí),執(zhí)行此操作的最佳位置是在您的容器視圖控制器中:
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;
}
重要:
您必須確保加載到容器中的視圖從上到下受到約束,并且您需要將其中一個(gè)垂直約束的優(yōu)先級(jí)設(shè)置為低于 1000 的值.(始終使用底部約束.)這是必要的,因?yàn)榉駝t Interface Builder 會(huì)抱怨 - 有一個(gè)很好的理由:
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:
在設(shè)計(jì)時(shí),您的根視圖具有固定的大小(高度).現(xiàn)在,如果您的所有子視圖都具有固定高度并與所有具有相同優(yōu)先級(jí)的固定約束連接,則不可能滿足所有這些要求,除非您的固定根視圖的高度恰好與子視圖的總高度和垂直約束完全匹配.如果您將其中一個(gè)約束的優(yōu)先級(jí)降低到 999,Interface Builder 就會(huì)知道要打破哪個(gè)約束.然而,在運(yùn)行時(shí)——當(dāng) translatesAutoresizingMaskIntoConstraints
屬性按上述設(shè)置時(shí)——你的根視圖不再有固定的框架,系統(tǒng)將使用你的 999 優(yōu)先級(jí)約束.
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.
這篇關(guān)于在滾動(dòng)視圖中使用動(dòng)態(tài)大小的控制器調(diào)整容器視圖的大小的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!