【发布时间】:2015-03-18 14:24:17
【问题描述】:
我正在尝试为具有动态页数(每页一个主要子视图)的分页滚动视图设置自动布局。我的视图层次结构设置如下:
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(它们都被添加到滚动视图中的视图)上设置了以下约束:
在这一点上,我只是不确定是什么导致了这个问题。非常感谢任何见解!
【问题讨论】:
标签: ios objective-c uiscrollview autolayout