我在 IB 中有以下内容,并且两个视图都打开了“使用自动布局”并关闭了“调整子视图大小”。
我只是尝试将自动布局视图的实例添加到容器视图中,以便其边缘与其容器视图的边缘相交。容器视图启用了“使用自动布局”,并且高度相同,但宽度是其两倍。这是代码:
- (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 是在自动布局视图添加为子视图后自动创建的?在 didMoveToSuperview 中打印 Autolayout View 的约束时,它还不存在。如何解决这个问题?我想尝试在 IB 中的自动布局视图上创建约束,作为在构建时忽略的占位符,但 IB 为自动布局视图禁用了所有自动布局选项。
消除此约束的最简单方法是设置translatesAutoresizingMaskIntoConstraints
你的看法false
在 Swift 中(或NO
在 Objective-C 中)。您无需任何代码即可完成此操作。您可以在界面生成器(附加屏幕)中使用关键路径。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)