問題描述
我在使用自動布局約束時遇到了 UIScrollView 的問題.我有以下視圖層次結構,通過 IB 設置約束:
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 點,但可以通過單擊垂直擴展至 400 點的高度.ViewA 和 ViewB 通過更新它們的高度約束(從 200 到 400)來擴展.這是相應的片段:
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];
}];
我的問題是,如果兩個視圖都展開,我需要能夠滾動才能看到整個內容,而現在滾動不起作用.如何使用約束來更新滾動視圖以反映 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 ?
目前我能想到的唯一解決方案是在動畫后手動設置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?
謝謝
推薦答案
我使用如下純結構
-view
-scrollView
-view A
-view B
-Button
確保 Button(THE LAST view) 有一個約束(從其底部到父視圖的垂直間距,即滾動視圖),在這種情況下,無論您的視圖 A 和視圖發生什么變化B 是,scrollView 的高度會相應改變.
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.
我參考了這個很棒的在線圖書網站.
I reference to this great online book site.
只需閱讀創建滾動視圖"部分,您應該會有一個想法.
Just read the "Creating a scroll view" section, you should have an idea.
我在創建詳細視圖時遇到了類似的問題,并且使用具有自動布局的 Interface Builder 非常適合該任務!
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!
祝你好運!
(其他資源:
關于滾動視圖的自動布局的堆棧溢出討論.
iOS 6 有一個發行說明 談論對 UIScrollView 的自動布局支持.
iOS 6 has a Release Notes talking about Auto Layout support for UIScrollView.
關于滾動視圖的免費在線iOS 書籍說明.這實際上對我幫助很大!
Free online iOS book explanation about scroll view. This actually helped me a lot!
這篇關于具有動態高度的子視圖的自動布局 UIScrollView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!