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.
translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout 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.
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];
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.
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