意外的NSAutoresizingMaskLayoutConstraint从笔尖将UIView添加到自动布局情节提要场景中 [英] Unexpected NSAutoresizingMaskLayoutConstraint adding UIView from nib to autolayout storyboard scene

查看:142
本文介绍了意外的NSAutoresizingMaskLayoutConstraint从笔尖将UIView添加到自动布局情节提要场景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IB中具有以下功能,并且两个视图均启用了使用自动布局"功能,并禁用了调整子视图的大小".

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

我只是试图将Autolayout View的实例添加到容器视图,以使其边缘与容器视图的边缘相交.容器视图上启用了使用自动布局",并且高度相同,但宽度是其两倍.这是代码:

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]];
}

发生错误:

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

在将NSAutoresizingMaskLayoutConstraint添加为子视图之后,它会自动在Autolayout视图上创建吗?在didMoveToSuperview中打印自动版式视图的约束时,该约束尚不存在.如何解决?我想尝试在IB中作为占位符在IB中的自动布局视图上创建约束,但在IB时禁用了自动布局视图的所有自动布局选项.

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.

推荐答案

删除此约束的最简单方法是将视图的translatesAutoresizingMaskIntoConstraints设置为Swift中的false(或Objective-C中的NO ).您完全可以不需要任何代码.您可以在界面生成器中使用键路径(附加屏幕).

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

这篇关于意外的NSAutoresizingMaskLayoutConstraint从笔尖将UIView添加到自动布局情节提要场景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆