简体   繁体   English

意外的NSAutoresizingMaskLayoutConstraint从笔尖添加UIView到自动布局故事板场景

[英]Unexpected NSAutoresizingMaskLayoutConstraint adding UIView from nib to autolayout storyboard scene

I've got the following in IB and both views have 'Use Auto Layout' on and 'Resizes Subviews' off. 我在IB中有以下内容,两个视图都打开了“使用自动布局”和“调整子视图大小”。

自动布局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. 我只是尝试将一个Autolayout View实例添加到容器视图中,使其边缘与容器视图的边缘相交。 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? 看起来NSAutoresizingMaskLayoutConstraint是在作为子视图添加后在Autolayout View上自动创建的? When printing Autolayout View's constraints in didMoveToSuperview, it doesn't exist yet. 在didMoveToSuperview中打印Autolayout View的约束时,它尚不存在。 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. 我想尝试在IB中创建对Autolayout View的约束作为占位符在构建时忽略,但IB为Autolayout View禁用了所有自动布局选项。

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). 删除此约束的最简单方法是在Swift中将视图的translatesAutoresizingMaskIntoConstraints设置为false (或在Objective-C中设置为NO )。 You can to this without any code at all. 你可以在没有任何代码的情况下使用它。 You can use key paths in interface builder (screen attached). 您可以在界面构建器中使用键路径(连接屏幕)。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM