Search code examples
iosobjective-cswiftnslayoutconstraintautolayout

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?


Solution

  • 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];