gpt4 book ai didi

ios - 当我扩展我的 UIViewController 时,我收到 NSLayoutConstraint 错误

转载 作者:行者123 更新时间:2023-11-28 23:34:45 24 4
gpt4 key购买 nike

当我扩展我的 UIViewController 并调用扩展的 ViewController 时,我得到:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“NSLayoutConstraint for (null): Constraint must contain a first layout item”错误。

 CGSize size = CGSizeMake(142, 200);
[self.scrollView.subviews enumerateObjectsUsingBlock:^(UIView* subView, NSUInteger i, BOOL *stop) {
subView.translatesAutoresizingMaskIntoConstraints = NO;
[ViewHelper addWidthConstraint:subView width:size.width];
[ViewHelper addHeightConstraint:subView height:size.height];
if (i < self.scrollView.subviews.count - 1) {
[ViewHelper addHorizontalConstraint:self.scrollView
previouseView:subView
nextView:(UIView*)self.scrollView.subviews[i + 1]
spacer:8];
}

[ViewHelper addEdgeConstraint:NSLayoutAttributeTop
superview:self.scrollView
subview:subView];

[ViewHelper addEdgeConstraint:NSLayoutAttributeBottom
superview:self.scrollView
subview:subView];

}];

[ViewHelper addEdgeConstraint:NSLayoutAttributeLeft
superview:self.scrollView
subview:self.scrollView.subviews.firstObject];

[ViewHelper addEdgeConstraint:NSLayoutAttributeRight
superview:self.scrollView
subview:self.scrollView.subviews.lastObject];

[ViewHelper addHeightConstraint:self.scrollView height:size.height];

在这一行崩溃:

[ViewHelper addEdgeConstraint:NSLayoutAttributeLeft
superview:self.scrollView
subview:self.scrollView.subviews.firstObject];

最佳答案

该错误表明您正在尝试使用为零的 View 添加约束。在尝试向 self.scrollView.subviews.firstObject/lastObject 添加约束之前,您应该防止 self.scrollView.subviews.firstObject/lastObject 为零(通过确保 ScrollView 具有任何 subview )。这是一个例子:

// make sure that the scrollView has some subviews before attempting to add layout constraints using the subviews
if ([self.scrollView.subviews count] > 0) {
[ViewHelper addEdgeConstraint:NSLayoutAttributeLeft
superview:self.scrollView
subview:self.scrollView.subviews.firstObject];

[ViewHelper addEdgeConstraint:NSLayoutAttributeRight
superview:self.scrollView
subview:self.scrollView.subviews.lastObject];
}

关于ios - 当我扩展我的 UIViewController 时,我收到 NSLayoutConstraint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55287750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com