美文网首页
NSDictionaryOfVariableBindings

NSDictionaryOfVariableBindings

作者: 石玉龙 | 来源:发表于2017-01-15 20:28 被阅读566次

在代码中使用Autolayout时,大家都会使用NSDictionaryOfVariableBindings这个宏,这个宏可以生成一个变量名到变量值映射的Dictionary。比如NSDictionaryOfVariableBindings(button1, button2)将会生成一个{ @”button1” = button1, @”button2 = button2 }的Dictionary。

- (UIView *)createBottomView {

UIView *bottomView = [[UIView alloc] init];

[bottomView setTranslatesAutoresizingMaskIntoConstraints:NO];

//resetButton

UIButton *resetButton = [[UIButton alloc] init];

[resetButton setTranslatesAutoresizingMaskIntoConstraints:NO];

[resetButton addTarget:self action:@selector(clickResetButton:) forControlEvents:UIControlEventTouchUpInside];

[resetButton.titleLabel setFont:[UIFont systemFontOfSize:BOTTOM_BUTTON_FONT_SIZE]];

[resetButton setTitleColor:[UIColor hexColor:FILTER_BLACK_STRING] forState:UIControlStateNormal];

NSString *resetString = LocalString(@"sZYFilterReset");

if ([resetString isEqualToString:@"sZYFilterReset"]) {

resetString = @"Reset";

}

[resetButton setTitle:resetString forState:UIControlStateNormal];

[resetButton setBackgroundColor:[UIColor whiteColor]];

[bottomView addSubview:resetButton];

//commitButton

UIButton *commitButton = [[UIButton alloc] init];

[commitButton setTranslatesAutoresizingMaskIntoConstraints:NO];

[commitButton addTarget:self action:@selector(clickCommitButton:) forControlEvents:UIControlEventTouchUpInside];

[commitButton.titleLabel setFont:[UIFont systemFontOfSize:BOTTOM_BUTTON_FONT_SIZE]];

[commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

NSString *commitString = LocalString(@"sZYFilterCommit");

if ([commitString isEqualToString:@"sZYFilterCommit"]) {

commitString = @"Commit";

}

[commitButton setTitle:commitString forState:UIControlStateNormal];

[commitButton setBackgroundColor:[UIColor hexColor:FILTER_RED_STRING]];

[bottomView addSubview:commitButton];

//constraints

NSDictionary *views = NSDictionaryOfVariableBindings(resetButton, commitButton);

[bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[resetButton][commitButton]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];

[bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[resetButton]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];

[bottomView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[commitButton]|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];

[bottomView addConstraint:[NSLayoutConstraint constraintWithItem:resetButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:commitButton attribute:NSLayoutAttributeWidth multiplier:1.f constant:0.f]];

return bottomView;

}

相关文章

网友评论

      本文标题:NSDictionaryOfVariableBindings

      本文链接:https://www.haomeiwen.com/subject/sfhubttx.html