12
votes

I've got the following in IB and both views have 'Use Auto Layout' on and 'Resizes Subviews' off.

autolayout uiview

I'm simply trying to add an instance of Autolayout View to a container view so its edges meet its container view's edges. The container view has Uses Auto Layout on and is the same height but width is twice as much. Here's the code:

- (IBAction)addSubviewButton:(id)sender
{
  UIView *autolayoutView = [[[NSBundle mainBundle] loadNibNamed:@"AutolayoutView" owner:nil options:nil] objectAtIndex:0];
  [self.containerView addSubview:autolayoutView];

  [self.containerView addConstraint:
   [NSLayoutConstraint constraintWithItem:autolayoutView
                            attribute:NSLayoutAttributeLeading
                            relatedBy:NSLayoutRelationEqual
                               toItem:self.containerView
                            attribute:NSLayoutAttributeLeading
                           multiplier:1
                             constant:0]];
  [self.containerView addConstraint:
   [NSLayoutConstraint constraintWithItem:autolayoutView
                            attribute:NSLayoutAttributeTrailing
                            relatedBy:NSLayoutRelationEqual
                               toItem:self.containerView
                            attribute:NSLayoutAttributeTrailing
                           multiplier:1
                             constant:0]];
  [self.containerView addConstraint:
  [NSLayoutConstraint constraintWithItem:autolayoutView
                           attribute:NSLayoutAttributeTop
                           relatedBy:NSLayoutRelationEqual
                              toItem:self.containerView
                           attribute:NSLayoutAttributeTop
                          multiplier:1.0
                            constant:0.0]];
  [self.containerView addConstraint:
  [NSLayoutConstraint constraintWithItem:autolayoutView
                           attribute:NSLayoutAttributeBottom
                           relatedBy:NSLayoutRelationEqual
                              toItem:self.containerView
                           attribute:NSLayoutAttributeBottom
                          multiplier:1.0 constant:0.0]];
}

An error occurs:

Probably at least one of the constraints in the following list is one you don't want...
(
"<NSIBPrototypingLayoutConstraint:0x8c364d0 'IB auto generated at build time for view with fixed frame' H:[UIView:0x8c356f0(275)]>",
"<NSLayoutConstraint:0x8d43d80 H:|-(0)-[AutolayoutView:0x8d47a30]   (Names: '|':UIView:0x8c356f0 )>",
"<NSLayoutConstraint:0x8d09260 AutolayoutView:0x8d47a30.trailing == UIView:0x8c356f0.trailing>",
"<NSAutoresizingMaskLayoutConstraint:0x8d0eca0 h=--& v=--& AutolayoutView:0x8d47a30.midX == + 70>"
)

Looks like NSAutoresizingMaskLayoutConstraint is created automatically on Autolayout View after it is added as a subview? When printing Autolayout View's constraints in didMoveToSuperview, it doesn't exist yet. How can this be resolved? I wanted to try creating constraints on Autolayout View in IB as placeholders to ignore at build time but IB has all of the autolayout options disabled for Autolayout View.

1
Check out the attributes of the top-level view in IB. There should be a checkbox for "Translates Autoresizing Mask Into Constraints". You need to turn that off. Alternatively, you can set the translatesAutoresizingMaskIntoConstraints property in code after loading the view.Ken Thomases
@KenThomases thanks for the tip. Turning off translatesAutoresizeMaskIntoConstraints in code worked. 'Use Autolayout' was the closest I could find in IB but the Autoresize constraint still was getting created.1192805

1 Answers

37
votes

The easiest way to remove this constraint will be to set translatesAutoresizingMaskIntoConstraints of your view to false in Swift (or to NO in Objective-C). You can to this without any code at all. You can use key paths in interface builder (screen attached).

enter image description here