动态UIScrollView内容:无法同时满足约束

时间:2015-03-14 17:26:17

标签: ios objective-c uiscrollview autolayout

我尝试为动态页数(每页一个主要子视图)的分页滚动视图设置自动布局。我的视图层次结构设置如下:

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上设置了以下约束(他们在滚动视图中添加了所有视图):

Constraints on content view

此时,我只是不确定导致问题的原因。非常感谢任何见解!

1 个答案:

答案 0 :(得分:0)

问题似乎是你改变常量的第一个约束

self.iphoneSVContentWConstr.constant = (self.subVWidth * self.contentSV.frame.size.width);

使用此约束设置的视图宽度的值(来自冲突约束输出的102400)与4 * 320(其中的视图宽度)不同。

如果滚动视图内的UIView的宽度完全由其内部的子视图的数量(已经定义了宽度)定义,则不应该需要此约束self.iphoneSVContentWConstr,因为您有足够的约束宽度可以毫不含糊地计算。

如果您仍想保留此宽度约束,请确保将常量设置为正确的值,例如:

self.iphoneSVContentWConstr.constant = (self.subVWidth * self.viewArr.count);

我想你在故事板中设置的滚动视图的宽度/高度。这应该足以使它工作,并且限制不再崩溃。

我真的很喜欢你构建视觉格式字符串的方式!酷:)

让我知道它是怎么回事。祝你好运!