Here we see an example of how to move an entire UIView with auto layout.
The idea here is to:

  1. Create a UIView
  2. Add some controls to it
  3. Make something happen.
  4. And then move the entire view by adjusting the value of a constraint.

First create a new single page application and drag a UIView onto it.

Screen Shot 2016-09-28 at 7.30.02 AM.png

Give it a background colour of green. Stretch it out so it locks to the sides of the parent view.

Then add have autolayout automatically add some constraints to it by clicking

Screen Shot 2016-09-28 at 7.37.57 AM.png

Screen Shot 2016-09-28 at 7.38.07 AM.png

This will automatically add the necessary constrains for us. Including the top one which we want to manipulate.

Then drag the top constraint over as an outlet, and add two buttons to reset it’s size programatically.

The buttons are going to reside inside our new UIView

Screen Shot 2016-09-28 at 7.39.59 AM.png

We are going to slide the whole view up/down by pressing the buttons and manipulating the value of the top constraints.

Add constraints to the Up/Down buttons so they stay embedded in the UIView like so

Screen Shot 2016-09-28 at 7.40.55 AM.png

Screen Shot 2016-09-28 at 7.41.26 AM.png

Screen Shot 2016-09-28 at 7.41.36 AM.png

Screen Shot 2016-09-28 at 7.42.03 AM.png

Your constraints should now look like this.

Screen Shot 2016-09-28 at 7.42.20 AM.png

Now the fun part. Add code to slide the entire UIView up and down.

Do this by adding actions for the up/down buttons and the following code.

If you want to get fancy you can add an animation like in the down button.

All the code and everything should eventually look like this.

Screen Shot 2016-09-28 at 7.50.09 AM.png


#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIView *baseView;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect newFrame = self.baseView.frame;

    newFrame.size.width = [[UIScreen mainScreen] bounds].size.width;
    newFrame.size.height = [[UIScreen mainScreen] bounds].size.height;
    [self.baseView setFrame:newFrame];

    self.baseView.userInteractionEnabled = YES;

- (IBAction)upPressed:(UIButton *)sender {
    NSLog(@"Up pressed");
    self.topConstraint.constant = 0;
    [self.view layoutIfNeeded];

- (IBAction)downPressed:(UIButton *)sender {
    NSLog(@"Down pressed");

    [UIView animateWithDuration:0.5
                         self.topConstraint.constant = 100;
                         [self.view layoutIfNeeded];


And when you run it and press the up/down buttons, the whole thing should like up/down.

Screen Shot 2016-09-28 at 7.51.34 AM.png

Screen Shot 2016-09-28 at 7.51.39 AM.png