可视化格式语言,按钮宽度和高度约束

时间:2014-07-09 12:42:58

标签: ios objective-c swift nslayoutconstraint nsautolayout

有没有办法用NSLayoutConstraints设置UIView的宽度和高度? 例如,我有以下约束设置

subView.setTranslatesAutoresizingMaskIntoConstraints(false);
        let cons:NSArray =   NSLayoutConstraint.constraintsWithVisualFormat("[subView(99)]", options:nil, metrics: nil, views: ["subView":subView]);
        self.view.addConstraints(cons);

这只设置宽度,如何添加高度?

2 个答案:

答案 0 :(得分:11)

两种可能的选择:

[view setTranslatesAutoresizingMaskIntoConstraints:NO];
NSArray* vConstraints = [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(25)]|" options:0 metrics:nil views:view]];
[parentView addConstraints:vConstraints];

NSLayoutConstraint* vConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25];
[parentView addConstraint:vConstraint];

答案 1 :(得分:6)

对于垂直布局,只需将 V:添加到VFL字符串的开头:

  subView.setTranslatesAutoresizingMaskIntoConstraints(false);

  let horizontalCons:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("[subView(99)]", options:nil, metrics: nil, views: ["subView":subView]);
  let veritcleCons:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:[subView(50)]", options:nil, metrics: nil, views: ["subView":subView]);

  self.view.addConstraints(horizontalCons);
  self.view.addConstraints(veritcleCons);