Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding constraint to the "main view" in a xib

I have a UIView defined in a .xib file. I need to set translatesAutoresizingMaskIntoConstraints = NO. This means that the frame is not translated to constraints so I need to set the size constraints by myself.

I have created a working category method for a UIView:

-(NSArray*)setSizeConstraints:(CGSize)size
{
    NSLayoutConstraint* height = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1  constant:size.height];
    NSLayoutConstraint* width = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1  constant:size.width];

    [self addConstraint:height];
    [self addConstraint:width];

    return [NSArray arrayWithObjects:height, width, nil];
}

But I would like to set these constraints from the Xcode interface builder, but all my AutoLayout controls are greyed out: Xcode interface builder

Is there a way to do this in the interface builder?

like image 866
ThomasCle Avatar asked Jul 02 '14 09:07

ThomasCle


1 Answers

This actually is possible.

Simply put the view you'd like to constrain into another view, like the highlighted view here. enter image description here

Then, add your desired constraints.

Finally, pull the view you just added constraints to out of its parent view. You can now add constraints to that view.

enter image description here

like image 140
Hunter Monk Avatar answered Oct 07 '22 01:10

Hunter Monk