上一篇文章提到了苹果已经创建了Visual Format language(可视化格式语言),可以用来实现自动布局。
这是Visual Format Language 的表达式,被写成了NSString。
Options
这是一个NSLayoutFormatOption 类型的参数。对于Visual Format Language,我们通常将这个参数设置为0。
Metrics
一个字典常量值,将会在Visual Format Language 表达式中使用到。为了简单一点,目前我们将会传入nil 值。
Views
这是一个视图字典,你将会在这个方法的第一个参数写限制条件。为了构造这个字典,简单地使用NSDIctionaryOfVaribleBindings C 函数并传递你的视图到这个方法里。它将会为你构造这个字典。在这个字典的关键字是视图的名字,你将会在方法的第一个参数中使用它。
/*Email text Field constraints*/ NSString *const KEmailTextFieldHorizontal = @"H:|-[_textFieldEmail]-|"; NSString *const KEmailTextFieldVertical = @"V:|-[_textFieldEmail]"; /*Confirm email text field constraints */ NSString *const KConfirmEmailHorizontal = @"H:|-[_textFieldConfirmEmail]-|"; NSString *const KConfirmEmailVertical = @"V:[_textFieldEmail]-[_textFieldConfirmEmail]"; /*Redister button constraint */ NSString *const KRegisterVertical = @"V:[_textFieldConfirmEmail]-[_registerButton]";
@interface ViewController () @property(nonatomic,strong) UIButton *button; @property (nonatomic,strong)UITextField *textFieldEmail; @property (nonatomic,strong)UITextField *textFieldConfirmEmail; @property (nonatomic,strong)UIButton *registerButton; @end
-(UITextField *)textFieldWithPlaceholder:(NSString *)paramPlaceholder{ UITextField *result = [[UITextField alloc] init]; result.translatesAutoresizingMaskIntoConstraints = NO; result.borderStyle = UITextBorderStyleRoundedRect; result.placeholder = paramPlaceholder; return result; } -(void)constructUIComponents { self.textFieldEmail = [self textFieldWithPlaceholder:@"Email"]; self.textFieldConfirmEmail =[self textFieldWithPlaceholder:@"Confirm Email"]; self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.registerButton setBackgroundColor:[UIColor blackColor]]; self.registerButton.translatesAutoresizingMaskIntoConstraints = NO; [self.registerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.registerButton setTitle:@"Register" forState:UIControlStateNormal]; }
- (void) addUIComponentsToView:(UIView *)paramView { [paramView addSubview:self.textFieldEmail]; [paramView addSubview:self.textFieldConfirmEmail]; [paramView addSubview:self.registerButton]; }
- (NSArray *) emailTextFieldConstraints { NSMutableArray *result = [[NSMutableArray alloc] init]; NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_textFieldEmail);//?? [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:KEmailTextFieldHorizontal options:0 metrics:nil views:viewsDictionary]]; [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:KEmailTextFieldVertical options:0 metrics:nil views:viewsDictionary]]; return [NSArray arrayWithArray:result]; } -(NSArray *)confirmEmailTextFieldConstraints { NSMutableArray *result = [[NSMutableArray alloc] init]; NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(_textFieldConfirmEmail,_textFieldEmail); [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:KConfirmEmailHorizontal options:0 metrics:nil views:viewDictionary]]; [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:KConfirmEmailVertical options:0 metrics:nil views:viewDictionary]]; return [NSArray arrayWithArray:result]; } - (NSArray *) registerButtonConstraints{ NSMutableArray *result = [[NSMutableArray alloc] init]; NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_registerButton, _textFieldConfirmEmail); [result addObject: [NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f] ]; [result addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat:KRegisterVertical options:0 metrics:nil views:viewsDictionary] ]; return [NSArray arrayWithArray:result]; } - (NSArray *) constraints { NSMutableArray *result = [[NSMutableArray alloc] init]; [result addObjectsFromArray:[self emailTextFieldConstraints]]; [result addObjectsFromArray:[self confirmEmailTextFieldConstraints]]; [result addObjectsFromArray:[self registerButtonConstraints]]; return [NSArray arrayWithArray:result]; }
- (void)viewDidLoad { [super viewDidLoad]; [self constructUIComponents]; [self addUIComponentsToView:self.view]; [self.view addConstraints:[self constraints]]; } /* Suport rotation of device to all orientation */ -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; }