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?
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];
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With