Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual formatting language, button width and height constraint

Is there any way to set UIView width and height with NSLayoutConstraints? for example i have the following constraint setup

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

this sets only width, how to add also a height?

like image 810
taffarel Avatar asked Jul 09 '14 12:07

taffarel


2 Answers

Two possible options:

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

or

NSLayoutConstraint* vConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:25];
[parentView addConstraint:vConstraint];
like image 123
Amit Avatar answered Sep 17 '22 20:09

Amit


For vertical layouts, simply add V: to the start of the VFL string:

  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);
like image 38
Yatheesha Avatar answered Sep 18 '22 20:09

Yatheesha