当superview正确更改时,无法更改子视图的大小

时间:2016-07-19 08:13:30

标签: ios swift uiview

我有更多的对象,但我猜我基本上有两个视图和一个按钮。其他的根本不影响那些对象。

expandingView位于屏幕的上方,而shinkingView位于屏幕的下方。它们相隔8个像素。

通过单击按钮,我想将expandingView的高度增加30,而另一个的高度减少30,这样我可以在有限的区域内获得一个更大而另一个更小。我编码如下:

expandingView.frame.size.height += 30
shrinkingView.frame.size.height -= 30

shrinkingView.frame.origin.y += 30

问题在于我无法在shrinkingView中更改子视图。其中一个需要修复,所以没关系,但另一个确实需要改变,因为它的superview改变了它的大小。

子视图与UIButton变量正确链接。我输入代码的方式与更改超级视图的方式相同,但没有任何更改,就好像我输入的代码不存在一样。从字面上看,它们根本没有任何影响。

即使使用相同的代码方式,为什么不能更改它们?它是因为它们处于不同的层次结构中?

1 个答案:

答案 0 :(得分:1)

//takes in a view and for each subview (so every element inside if it is some kind of child of the view, it will be reduces. So all elements will be reduced. 
func reduceSubviews(view:UIView) {
    for views in view.subviews {
        views.frame.size.height -= 30
        if(views.subviews.count > 0) {
            reduceSubviews(views)
        }
    }
}
//apply to any view such as shrinkingView
reduceSubviews(shrinkingView)

嗯,坦率地说,这段代码转到了缩小视图,通过它的所有子视图,并降低了它们的高度。或者它应该lol

相关问题