Haagenti
Haagenti

Reputation: 8144

iOS View is not expanding the way I want it

I have a UIViewController with a UICollectionView and a UIView at the bottom. The way I put it together is displayed in the image below

enter image description here

The yellow square is the UICollectionView and the red is the UIView. This works out just fine. But now I want to resize the UIView because it sometimes contains more info and needs to be bigger. So I tried this:

[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height + 10)];

But this expands the UIView at the bottom and it is not visible. I guess this is because I do not have the correct constraints? I also tried to subtract the origin.y with the same amount and this works only the UICollectionView doesn't get resized with the new height. So how do I tackle this problem?

Upvotes: 0

Views: 112

Answers (2)

zbMax
zbMax

Reputation: 2818

When setting constraints on your storyboard or in a xib file, animations perform animations on the constraints instead of the sizes and positions.

First create a outlet reference of the constraint which will change (in your case the top space of your UIView to the top layout guide) in the header file of your view controller.

When you want to animate a view, you now have to update its constraints and ask to layout the views.
For example :

[UIView animateWithDuration:0.2
                         animations:^{
                             viewYConstraint.constant -= 44;
                             [self.view layoutIfNeeded];
                         }
]
//Now don't forget to update constraints
[self.view updateConstraints];

Upvotes: 0

Levi
Levi

Reputation: 7343

If you are using autolayout, you should not be setting the frame from your code. Instead you should modify the constant of a constraint that is causing your view to be the incorrect size. You can have an IBOutlet to that constraint and you can change it's constant property. Then call setNeedsLayout on your view controller's view

Upvotes: 2

Related Questions