Unexpected NSAutoresizingMaskLayoutConstraint adding UIView from nib to autolayout storyboard scene

↘锁芯ラ 提交于 2019-12-12 08:25:33

问题


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

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:


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).



来源:https://stackoverflow.com/questions/25101957/unexpected-nsautoresizingmasklayoutconstraint-adding-uiview-from-nib-to-autolayo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!