Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my programmatically created view ignore its constraints?

I have a view that programmatically creates a subview and adds some constraints to it. For some reason, the constraints are being completely ignored. No error is thrown. The constraints simply do not take effect. The subview has the same frame as it's superview. I'm expecting it to be 400x400 and centered in its superview. What am I doing wrong here? Here's my initWithFrame in the superview:

- (id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        instructionsView = [[UIView alloc] initWithFrame:frame];
        [self addSubview:instructionsView];
        [self bringSubviewToFront:instructionsView];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                         attribute:NSLayoutAttributeCenterX
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterX
                                                        multiplier:1.
                                                          constant:0]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                         attribute:NSLayoutAttributeCenterY
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self
                                                         attribute:NSLayoutAttributeCenterY
                                                        multiplier:1.
                                                          constant:0]];

        [instructionsView addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                                     attribute:NSLayoutAttributeWidth
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:nil
                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                    multiplier:1.
                                                                      constant:400]];

        [instructionsView addConstraint:[NSLayoutConstraint constraintWithItem:instructionsView
                                                                     attribute:NSLayoutAttributeHeight
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:nil
                                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                                    multiplier:1.
                                                                      constant:400]];
    }
    return self;
}

I have tried changing

instructionsView = [[UIView alloc] initWithFrame:frame];

to

instructionsView = [[UIView alloc] initWithFrame:CGRectZero];

with no success.

like image 769
Ben H Avatar asked Feb 10 '14 20:02

Ben H


People also ask

What does translatesAutoresizingMaskIntoConstraints mean?

translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout constraints.

How do I turn off constraints?

To disable a foreign key constraint for INSERT and UPDATE statements. In Object Explorer, expand the table with the constraint and then expand the Keys folder. Right-click the constraint and select Modify. In the grid under Table Designer, select Enforce Foreign Key Constraint and select No from the drop-down menu.


2 Answers

You need to tell the view not to use autoresizing mask based layout and instead use autolayout

instructionsView.translatesAutoresizingMaskIntoConstraints = NO;

See the documentation here

Also, the view should be created without a frame, this is mostly a style thing but create your view like this:

instructionsView = [[UIView alloc] init];
like image 174
danielbeard Avatar answered Oct 16 '22 23:10

danielbeard


Ambiguity can cause surprising results without raising any exception. To debug constraints for ambiguity, pause in the debugger and type at the lldb command line:

po [[UIWindow keyWindow] _autolayoutTrace]

Also, you may (and should) add a method (through a category) on UIView that lets you call hasAmbiguousLayout recursively down the hierarchy.

Finally, you may (and should) add a method (through a category) on UIView that lets you call constraintsAffectingLayoutForOrientation: recursively down the hierarchy.

In this way you can track down what's really happening.

like image 40
matt Avatar answered Oct 17 '22 00:10

matt