【问题标题】:Dynamic UIScrollView content: Unable to simultaneously satisfy constraints动态 UIScrollView 内容:无法同时满足约束
【发布时间】: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


    【解决方案1】:

    问题似乎是您更改常量的第一个约束

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

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

    如果滚动视图中的 UIView 的宽度完全由它内部的子视图的数量定义(已经定义了宽度),你应该不需要这个约束self.iphoneSVContentWConstr,因为你有足够的宽度约束计算清楚。

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

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

    我想你在情节提要中设置的滚动视图的宽度/高度。这应该足以让它工作并且约束不再崩溃。

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

    让我知道进展如何。祝你好运!

    【讨论】:

    • 您好,Catalina,感谢您的回复!我可能应该更清楚 - iphoneSVContentWConstr 约束是视图的 width 约束,它应该定义滚动视图的内容大小(它是滚动视图内部的视图)。对此有什么想法吗?
    • 很抱歉。我已经用另一个解决方案更新了我的答案
    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-28
    • 2014-10-27
    • 2014-06-19
    相关资源
    最近更新 更多