具有动态高度的子视图的自动布局 UIScrollView

Auto layout UIScrollView with subviews with dynamic heights(具有动态高度的子视图的自动布局 UIScrollView)
本文介绍了具有动态高度的子视图的自动布局 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!

这篇关于具有动态高度的子视图的自动布局 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)