代码之家  ›  专栏  ›  技术社区  ›  rebello95

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

  •  0
  • rebello95  · 技术社区  · 11 年前

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

    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 回复  |  直到 11 年前
        1
  •  0
  •   Catalina T. dhiraj kakran    11 年前

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

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

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

    如果滚动视图中UIView的宽度完全由其内部的子视图的数量(已经定义了宽度)定义,则不需要此约束 self.iphoneSVContentWConstr ,因为您有足够的约束来明确计算宽度。

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

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

    我想是您在故事板中设置的滚动视图的宽度/高度。这应该足以让它工作,让约束不再崩溃。

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

    告诉我进展如何。祝你好运