自动布局错误 Unable to simultaneously satisfy constraints with UIScrollView

Auto layout error Unable to simultaneously satisfy constraints with UIScrollView

我有一个用于显示 UIImageView 的 UIScrollView。根据用户先前保存的图像数量,在运行时以编程方式插入 ImageView。我收到以下错误:

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:0x17029cd90 H:[UIView:0x15cd31e70]-(0)-|   (Names: '|':UIScrollView:0x15cd326b0 )>",
"<NSLayoutConstraint:0x17029d060 UIScrollView:0x15cd326b0.trailingMargin == UIView:0x15cd31e70.trailing>"

我做了基本的自动布局,其中滚动视图在 0 点固定到所有四个边。然后我添加一个 contentView 作为 UIScrollView 的子视图(纯 UIView),它也固定在所有四个边的 0 点。

编辑故事板约束图像

我在代码中给 contentView 一个宽度,如下所示:

    CGSize pagesScrollViewSize = self.scrollView.frame.size;

        NSDictionary *views     = @{ @"contentView"       :   self.contentView};

        NSDictionary *metrics   = @{ @"width"           :   @(pagesScrollViewSize.width * self.mutableArray.count) };

        NSArray *constraints;
        constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView(width)]-|" options:0 metrics:metrics views:views];
        [self.scrollView addConstraints:constraints];

[self loadVisiblePages];

UIImageViews 是这样添加的,其中 UIImageViews 是根据当转至 ViewController 时设置的页数添加的。

-(void)loadVisiblePages{

        CGRect frame = self.scrollView.bounds;
        frame.origin.x = frame.size.width * self.page;
        frame.origin.y = 0.0f;

        ImageForArchiving *newImageObject = (ImageForArchiving*)self.mutableArray[page];
        UIImage *imageForNewPageView = newImageObject.image;

        UIImageView *newPageView = [[UIImageView alloc] initWithFrame:frame];
        [newPageView setTranslatesAutoresizingMaskIntoConstraints:YES];
         newPageView.contentMode = UIViewContentModeScaleAspectFit;
        newPageView.image = imageForNewPageView;
        [self.scrollView addSubview:newPageView];

        [self.pageViews replaceObjectAtIndex:page withObject:newPageView];
    }
}

此外,当我滚动 UIScrollView 时,显示的图像在旋转时会不规律地改变大小。我认为这只是上述警告和我尚未布置 UIImageViews 的事实的结果。上述警告在这种情况下是什么意思,我该如何解决?

您似乎已将 scrollView 的 trailingMargin 固定到 contentView.trailing。 将此约束的 scrollView.Trailing 边距更改为 scrollView.Trailing 选择约束后,您可以在故事板的 activity 检查器中执行此操作。

或者,清除对 contentView 的所有约束。然后在再次添加固定约束时取消选中 Constrain to margins 并将所有常量设置为 0.

AND

更改代码中的这一行

NSArray *constraints;
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[contentView(width)]-|" options:0 metrics:metrics views:views];
    [self.scrollView addConstraints:constraints];

有了这个:

NSArray *constraints;
    constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView(width)]|" options:0 metrics:metrics views:views];
    [self.scrollView addConstraints:constraints];

在视觉格式中使用 @"H:|-[contentView(width)]-|" 意味着将您的 contentView 固定到 superView 的边距,并在 superView 和 subView 之间添加 8 pts 的 space。在您的情节提要约束中,您已经使用 UIScrollView 的后缘和前缘设置了约束,而在以编程方式添加的约束中,您使用了尾随边距和前导边距(有点要求 contentView 保持 8 磅的填充)。因此,冲突。

检查视觉格式语法 here