具有动态高度的子视图的自动布局 UIScrollView
问题说明
我在使用自动布局约束时遇到了 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!
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangicahb
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20