Created
April 9, 2014 10:28
-
-
Save jsleeuw/10252211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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