【发布时间】:2013-05-25 09:07:52
【问题描述】:
我在使用自动布局约束时遇到了 UIScrollView 的问题。 我有以下视图层次结构,通过 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)来扩展。这是对应的sn-p:
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 高度的变化?
目前我能想到的唯一解决方案是在动画后手动设置ContainerView的高度,这将是ViewA + ViewB + Button的高度之和。但我相信有更好的解决方案?
谢谢
【问题讨论】:
标签: ios objective-c uiscrollview autolayout