Alex Stone
Alex Stone

Reputation: 47348

iOS how to activate/deactivate layout constraints for a certain size class (orientation)?

I have an iOS app universal interface designed using size classes and autolayout. Within the app I'm doing constraint based animations by activating and deactivating constraints.

The following approach works in "portrait", however, I have optional constraints installed for "landscape" size class. How do I account for the current screen size class when activating/deactivating constraints for animation?

For example, the image I have I want my animation code to know if it should activate some set of constraints, but not others.

-(NSArray*)layoutFullScreen
{
    return @[self.imageYCenterConstraint,
             self.imageWidthRatioConstraint];
}

-(NSArray*)layoutWorkingScreen
{
    return  @[self.textLabelCenterYConstraint,
              self.imageHeightRatioConstraint];
}

-(void)doAnimation
{
        [NSLayoutConstraint deactivateConstraints:[self layoutFullScreen]];
        [NSLayoutConstraint activateConstraints:[self layoutWorkingScreen]];

        [UIView animateWithDuration:0.6 delay:0 options:0 animations:^{
            [self.view layoutIfNeeded];

        } completion:^(BOOL finished) {
        }];
}

Upvotes: 1

Views: 815

Answers (1)

Antonis
Antonis

Reputation: 104

If you are using size classes, you must not think in terms of rotation, but in terms of UITraitsCollections. You can handle the size class change in traitCollectionDidChange: and fix your constraints accordingly.

Upvotes: 1

Related Questions