Skip to content

Instantly share code, notes, and snippets.

@jsleeuw
Created April 9, 2014 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsleeuw/10252211 to your computer and use it in GitHub Desktop.
Save jsleeuw/10252211 to your computer and use it in GitHub Desktop.
#import "AutoLayoutVFLViewController.h"
@interface AutoLayoutVFLViewController ()
@property (strong, nonatomic) UILabel *label;
- (void)animateLabel;
@end
@implementation AutoLayoutVFLViewController
- (void)loadView
{
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIView *mainView = [[UIView alloc] initWithFrame:applicationFrame];
mainView.backgroundColor = [UIColor whiteColor];
self.view = mainView;
self.label = [UILabel new];
self.label.text = @"This is a Label";
self.label.font = [UIFont systemFontOfSize:18];
self.label.backgroundColor = [UIColor lightGrayColor];
self.label.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.label];
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-offsetTop-[_label]"
options:0
metrics:@{@"offsetTop": @100}
views:NSDictionaryOfVariableBindings(_label)];
[self.view addConstraints:constraints];
UIView *spacer1 = [UIView new];
spacer1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:spacer1];
UIView *spacer2 = [UIView new];
spacer2.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:spacer2];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[spacer1][_label][spacer2(==spacer1)]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(_label, spacer1, spacer2)]];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:@selector(animateLabel) withObject:nil afterDelay:2.0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)animateLabel {
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == self.label) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = 200.0;
}
}];
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment