用动态的高度子视图自动布局的UIScrollView [英] Auto layout UIScrollView with subviews with dynamic heights

查看:227
本文介绍了用动态的高度子视图自动布局的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 +按键的高度之和的高度。但我相信有一个更好的解决方案?

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?

感谢

推荐答案

我用纯粹的结构类似以下

I use pure structure like the following

-view
  -scrollView
    -view A
    -view B
    -Button

确认按钮(最后一个视图)有(从底部到上海华,这是滚动视图垂直间距)约束,在这种情况下,不管你的视图A和视图改变B.将,滚动视图的高度也随之改变。

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.

我有我创建一个详细视图和使用界面生成器具有自动布局类似的问题,就是这样一个非常适合的任务!

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!

祝你好运!

(其他资源:

堆栈溢出讨论<一个href=\"http://stackoverflow.com/questions/13499467/uiscrollview-doesnt-use-autolayout-constraints\">auto布局滚动视图。

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆