0

I have three views like so:

enter image description here

The grey view has a top constraint to "Header 1" and "Header 2" has a top constraint to the grey view.

I am animating the grey view like this:

    CGRect rect = self.fruitSection.frame;
    rect.size.height = 0;

    [UIView animateWithDuration:0.3 animations:^{
        [self.greySection setFrame:rect];
    }];

And this makes it slide up. However, when the grey view slides to height=0, the "Header 1" view below it stays where it is (and leaves whitespace between "Header 1" and "Header 2" where the grey view used to be).

Is there a way I can make "Header 2" slide up as the grey view collapses?

Thanks!

UPDATE

I've tried to animate the constraint instead of the actual view like so:

   [UIView animateWithDuration:0.5 animations:^{
        self.greyHeightConstraint.constant = 0.0f;
    }];

and this works, but the view isn't animated; it "jumps" instead of slides

user1282637
  • 1,827
  • 5
  • 27
  • 56
  • All I can think of is animating `constant` to `1.0f` and then in the completion block set the `constant` to `0.0f`. I'm not sure if setting to `0.0f` triggers an optimisation that stops an animation. – Robotic Cat Sep 26 '15 at 18:34
  • @RoboticCat same behavior unfortunately, thanks for the suggestion though – user1282637 Sep 26 '15 at 18:36
  • Have you read this question (How do I animate constraint changes?) and it's answers: http://stackoverflow.com/q/12622424/558933 – Robotic Cat Sep 27 '15 at 01:59

1 Answers1

1

I think you mixed up "Header 1" and "Header 2" in the description, but what you should do is animate the greySection's height constraint instead of its frame.

I guess the greysection has a constant height constraint, with a value of, say, 200.

You want to make an property for this constraint (an IBOutlet, i guess), and change the .constant property. Then call [self.view layoutIfNeeded] in the animation block.

Vlk
  • 99
  • 5
  • Thanks, I updated the question to fix that. Your answer sort-of works. It DOES push the "Header 1" up and removes the whitespace. However, the view is no longer animated, it just jumped. I've updated the question to show what I tried – user1282637 Sep 26 '15 at 18:28