0

I have two views on top of one another like so. When I press button, I want to hide the grey view and have the pink view move up into its space. When I press button again, I want the grey view to re-appear and move the pink view back into its original position.

enter image description here

I can accomplish this by doing something like

- (IBAction)toggle:(id)sender {
    if (self.top.hidden) {
        self.top.hidden = NO;
        self.top.layer.frame = CGRectMake(0, 0, 320, 50);
        self.bottom.layer.frame = CGRectMake(0, 50, 320, 50);
    } else {
        self.top.layer.frame = CGRectMake(0, 0, 0, 0);
        self.bottom.layer.frame = CGRectMake(0, 0, 320, 50);
        self.top.hidden = TRUE;
    }
}

However, the general wisdom, from what I understand, regarding autolayout is to not set the frame sizes programmatically in the code. So my question is, how can achieve the same result using autolayout?

It's basically the iPhone version of this question: OS X Cocoa Auto Layout hidden elements

Community
  • 1
  • 1
super9
  • 29,181
  • 39
  • 119
  • 172

1 Answers1

0

Found the answer courtesy of this question: Animating an image view to slide upwards

In short:

- (IBAction)toggle:(id)sender {
    if (self.top.hidden) {
        self.top.hidden = NO;
        [UIView animateWithDuration:0
                         animations:^{
                             self.verticalSpace.constant += 50.0;
                             [self.view layoutIfNeeded];
                         }];
    } else {
        [UIView animateWithDuration:0
                         animations:^{
                             self.verticalSpace.constant -= 50.0;
                             [self.view layoutIfNeeded];
                         }];
        self.top.hidden = TRUE;
    }
}

where self.verticalSpace is the constraint set between the bottom view to the top view.

Community
  • 1
  • 1
super9
  • 29,181
  • 39
  • 119
  • 172