动态 UIScrollView 内容:无法同时满足约束
Dynamic UIScrollView content: Unable to simultaneously satisfy constraints
我正在尝试为具有动态页数的分页滚动视图设置自动布局(每页一个主子视图)。我的视图层次结构设置如下:
Main view
Scroll view
UIView (fits content)
TutorialSubview
...
将所有视图添加到数组后,我有以下代码动态生成约束:
self.iphoneSVContentWConstr.constant = (self.subVWidth * self.contentSV.frame.size.width);
NSMutableDictionary *views = [NSMutableDictionary new];
NSLayoutFormatOptions formatForVert = (NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom);
NSMutableString *format = [NSMutableString stringWithString:@"|"];
int idx = 0; for (UIView *v in self.viewArr) {
NSString *key = [NSString stringWithFormat:@"View%i", idx];
[views setObject:v forKey:key];
[format appendFormat:@"[%@(%.f)]", key, self.subVWidth];
idx++;
}
[format appendString:@"|"];
NSLog(@"%@", format);
//Update the content view
[self.iphoneSVContent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:formatForVert metrics:nil views:views]];
[self.contentSV layoutIfNeeded];
这最终输出:
|[View0(320)][View1(320)][View2(320)][View3(320)]|
这似乎是正确的。但是,我收到以下错误:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x17409f1d0 H:[UIView:0x174197c40(102400)]>",
"<NSLayoutConstraint:0x1742811d0 H:|-(0)-[TutorialSubview:0x1743a1960] (Names: '|':UIView:0x174197c40 )>",
"<NSLayoutConstraint:0x170287490 H:[TutorialSubview:0x1743a1960(320)]>",
"<NSLayoutConstraint:0x1702873f0 H:[TutorialSubview:0x1743a1960]-(0)-[TutorialSubview:0x1743a3100]>",
"<NSLayoutConstraint:0x1702871c0 H:[TutorialSubview:0x1743a3100(320)]>",
"<NSLayoutConstraint:0x1702872b0 H:[TutorialSubview:0x1743a3100]-(0)-[TutorialSubview:0x1743a3480]>",
"<NSLayoutConstraint:0x170287210 H:[TutorialSubview:0x1743a3480(320)]>",
"<NSLayoutConstraint:0x170286fe0 H:[TutorialSubview:0x1743a3480]-(0)-[TutorialSubview:0x1703a31e0]>",
"<NSLayoutConstraint:0x170287080 H:[TutorialSubview:0x1703a31e0(320)]>",
"<NSLayoutConstraint:0x170286f40 H:[TutorialSubview:0x1703a31e0]-(0)-| (Names: '|':UIView:0x174197c40 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x170286fe0 H:[TutorialSubview:0x1743a3480]-(0)-[TutorialSubview:0x1703a31e0]>
我在 self.iphoneSVContent
上设置了以下约束(它们都被添加到滚动视图中的视图):
在这一点上,我只是不确定是什么导致了这个问题。非常感谢任何见解!
问题似乎是您更改常量的第一个约束
self.iphoneSVContentWConstr.constant = (self.subVWidth * self.contentSV.frame.size.width);
您使用此约束设置的视图宽度值(102400,来自冲突约束输出)与 4* 320(其中视图的宽度)不同。
如果scrollview里面的UIView的宽度完全由它里面的子视图的数量定义(已经定义了宽度),你应该不需要这个约束self.iphoneSVContentWConstr
,因为你有足够的约束用于明确计算宽度。
如果您仍想保持此宽度限制,请确保将常量设置为正确的值,例如:
self.iphoneSVContentWConstr.constant = (self.subVWidth * self.viewArr.count);
我想你在情节提要中设置的滚动视图的 width/height。这应该足以让它工作并且约束不再崩溃。
我真的很喜欢你构造视觉格式字符串的方式!酷 :)
让我知道进展如何。祝你好运!
我正在尝试为具有动态页数的分页滚动视图设置自动布局(每页一个主子视图)。我的视图层次结构设置如下:
Main view
Scroll view
UIView (fits content)
TutorialSubview
...
将所有视图添加到数组后,我有以下代码动态生成约束:
self.iphoneSVContentWConstr.constant = (self.subVWidth * self.contentSV.frame.size.width);
NSMutableDictionary *views = [NSMutableDictionary new];
NSLayoutFormatOptions formatForVert = (NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom);
NSMutableString *format = [NSMutableString stringWithString:@"|"];
int idx = 0; for (UIView *v in self.viewArr) {
NSString *key = [NSString stringWithFormat:@"View%i", idx];
[views setObject:v forKey:key];
[format appendFormat:@"[%@(%.f)]", key, self.subVWidth];
idx++;
}
[format appendString:@"|"];
NSLog(@"%@", format);
//Update the content view
[self.iphoneSVContent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:format options:formatForVert metrics:nil views:views]];
[self.contentSV layoutIfNeeded];
这最终输出:
|[View0(320)][View1(320)][View2(320)][View3(320)]|
这似乎是正确的。但是,我收到以下错误:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x17409f1d0 H:[UIView:0x174197c40(102400)]>",
"<NSLayoutConstraint:0x1742811d0 H:|-(0)-[TutorialSubview:0x1743a1960] (Names: '|':UIView:0x174197c40 )>",
"<NSLayoutConstraint:0x170287490 H:[TutorialSubview:0x1743a1960(320)]>",
"<NSLayoutConstraint:0x1702873f0 H:[TutorialSubview:0x1743a1960]-(0)-[TutorialSubview:0x1743a3100]>",
"<NSLayoutConstraint:0x1702871c0 H:[TutorialSubview:0x1743a3100(320)]>",
"<NSLayoutConstraint:0x1702872b0 H:[TutorialSubview:0x1743a3100]-(0)-[TutorialSubview:0x1743a3480]>",
"<NSLayoutConstraint:0x170287210 H:[TutorialSubview:0x1743a3480(320)]>",
"<NSLayoutConstraint:0x170286fe0 H:[TutorialSubview:0x1743a3480]-(0)-[TutorialSubview:0x1703a31e0]>",
"<NSLayoutConstraint:0x170287080 H:[TutorialSubview:0x1703a31e0(320)]>",
"<NSLayoutConstraint:0x170286f40 H:[TutorialSubview:0x1703a31e0]-(0)-| (Names: '|':UIView:0x174197c40 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x170286fe0 H:[TutorialSubview:0x1743a3480]-(0)-[TutorialSubview:0x1703a31e0]>
我在 self.iphoneSVContent
上设置了以下约束(它们都被添加到滚动视图中的视图):
在这一点上,我只是不确定是什么导致了这个问题。非常感谢任何见解!
问题似乎是您更改常量的第一个约束
self.iphoneSVContentWConstr.constant = (self.subVWidth * self.contentSV.frame.size.width);
您使用此约束设置的视图宽度值(102400,来自冲突约束输出)与 4* 320(其中视图的宽度)不同。
如果scrollview里面的UIView的宽度完全由它里面的子视图的数量定义(已经定义了宽度),你应该不需要这个约束self.iphoneSVContentWConstr
,因为你有足够的约束用于明确计算宽度。
如果您仍想保持此宽度限制,请确保将常量设置为正确的值,例如:
self.iphoneSVContentWConstr.constant = (self.subVWidth * self.viewArr.count);
我想你在情节提要中设置的滚动视图的 width/height。这应该足以让它工作并且约束不再崩溃。
我真的很喜欢你构造视觉格式字符串的方式!酷 :)
让我知道进展如何。祝你好运!