jycjyc13213213 jycjyc13213213 - 1 year ago 81
Swift Question

sizes of subviews cannot be changed while superview gets changed properly

I have some more objects but guess I basically have two views and one button. The others don't affect to those objects at all.

expandingView is on the upper side of the screen while the shinkingView is on the lower. They are 8 pixels apart.

By clicking the button, I want to increase the height of expandingView by 30 while that of the other decreases by 30 so that I can get one bigger and the other one smaller in a limited size of area. I coded like below:

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

shrinkingView.frame.origin.y += 30

What makes the problem is that I cannot change subviews in shrinkingView. One of them needs to be fixed so it's okay but the other one does need to be changed as its superview changes its size.

The subviews are linked correctly to UIButton variables. I typed the code in the same way as I change the superviews but nothing changed as if the codes I typed do not exist. Literally they have no effects at all.

Why can't they be changed even if the same way of codes are used? It it because they are in a different level of hierarchy?

Answer Source
//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) {
//apply to any view such as shrinkingView

Well, to be quite frank, this code goes to shrinkingView, gors through ALL of the subviews of it, and reduces their height. Or it should lol