Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
The implementation file for an example of animating a UIView using autolayout constraints.
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, weak) IBOutlet UIView *viewToAnimate;
@property (nonatomic, assign) BOOL containerIsOpen;
@implementation ViewController
#pragma mark - View Lifecycle
- (void)viewDidLoad
[super viewDidLoad];
self.containerIsOpen = NO;
#pragma mark - Interface Actions
- (IBAction)moveButtonTouched
if (self.containerIsOpen) {
[self replaceTopConstraintOnView:self.viewToAnimate withConstant:-220.0];
} else {
[self replaceTopConstraintOnView:self.viewToAnimate withConstant:0.0];
[self animateConstraints];
self.containerIsOpen = !self.containerIsOpen;
#pragma mark - Helper Methods
- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = constant;
- (void)animateConstraints
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];

This comment has been minimized.

Copy link

hhanesand commented Apr 21, 2015

I know this is quite old, but for anyone looking at this, you can just connect an outlet for the constraint you want to animate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.