問題描述
我在使用自動(dòng)布局約束時(shí)遇到了 UIScrollView 的問題.我有以下視圖層次結(jié)構(gòu),通過 IB 設(shè)置約束:
I'm having troubles with UIScrollView using auto layout constraints. I have the following view hierarchy, with constraints set through IB:
- ScrollView (leading, trailing, bottom and top spaces to superview)
-- ContainerView (leading, trailing, bottom and top spaces to superview)
--- ViewA (full width, top of superview)
--- ViewB (full width, below ViewA)
--- Button (full width, below ViewB)
ViewA 和 ViewB 的初始高度為 200 點(diǎn),但可以通過單擊垂直擴(kuò)展至 400 點(diǎn)的高度.ViewA 和 ViewB 通過更新它們的高度約束(從 200 到 400)來擴(kuò)展.這是相應(yīng)的片段:
The ViewA and ViewB have initial heights of 200 points, but it can be expended vertically to an height of 400 points by clicking on it. ViewA and ViewB are expanded by updating their height constraint (from 200 to 400). Here is the corresponding snippet :
if(self.contentVisible) {
heightConstraint.constant -= ContentHeight;
// + additional View's internal constraints update to hide additional content
self.contentVisible = NO;
} else {
heightConstraint.constant += ContentHeight;
// + additional View's internal constraints update to show additional content
self.contentVisible = YES;
}
[self.view setNeedsUpdateConstraints];
[UIView animateWithDuration:.25f animations:^{
[self.view layoutIfNeeded];
}];
我的問題是,如果兩個(gè)視圖都展開,我需要能夠滾動(dòng)才能看到整個(gè)內(nèi)容,而現(xiàn)在滾動(dòng)不起作用.如何使用約束來更新滾動(dòng)視圖以反映 ViewA 和 ViewB 高度的變化?
My problem is that if both views are expanded, I need to be able to scroll to see the whole content, and right now the scroll is not working. How can I manage to update the scroll view using constraints to reflect the changes of ViewA and ViewB heights ?
目前我能想到的唯一解決方案是在動(dòng)畫后手動(dòng)設(shè)置ContainerView的高度,這將是ViewA + ViewB + Button的高度之和.但我相信有更好的解決方案?
The only solution I can think of so far is to manually set the height of the ContainerView after the animation, which will be the sum of the heights of ViewA + ViewB + Button. But I believe there is a better solution?
謝謝
推薦答案
我使用如下純結(jié)構(gòu)
-view
-scrollView
-view A
-view B
-Button
確保 Button(THE LAST view) 有一個(gè)約束(從其底部到父視圖的垂直間距,即滾動(dòng)視圖),在這種情況下,無論您的視圖 A 和視圖發(fā)生什么變化B 是,scrollView 的高度會(huì)相應(yīng)改變.
Make sure Button(THE LAST view) has a constraint(vertical spacing from its bottom to superview, which is the scrollview), in this case, no matter what changes for your view A and view B would be, scrollView's height will be changed accordingly.
我參考了這個(gè)很棒的在線圖書網(wǎng)站.
I reference to this great online book site.
只需閱讀創(chuàng)建滾動(dòng)視圖"部分,您應(yīng)該會(huì)有一個(gè)想法.
Just read the "Creating a scroll view" section, you should have an idea.
我在創(chuàng)建詳細(xì)視圖時(shí)遇到了類似的問題,并且使用具有自動(dòng)布局的 Interface Builder 非常適合該任務(wù)!
I had the similar problem that I was creating a detail view and using Interface Builder with Auto layout is such a good fit for the task!
祝你好運(yùn)!
(其他資源:
關(guān)于滾動(dòng)視圖的自動(dòng)布局的堆棧溢出討論.
iOS 6 有一個(gè)發(fā)行說明 談?wù)搶?duì) UIScrollView 的自動(dòng)布局支持.
iOS 6 has a Release Notes talking about Auto Layout support for UIScrollView.
關(guān)于滾動(dòng)視圖的免費(fèi)在線iOS 書籍說明.這實(shí)際上對(duì)我?guī)椭艽螅?em class="showen">
Free online iOS book explanation about scroll view. This actually helped me a lot!
這篇關(guān)于具有動(dòng)態(tài)高度的子視圖的自動(dòng)布局 UIScrollView的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!