简体   繁体   English

自动布局错误无法同时满足 UIScrollView 的约束

[英]Auto layout error Unable to simultaneously satisfy constraints with UIScrollView

I have a UIScrollView that is to show UIImageViews.我有一个用于显示 UIImageViews 的 UIScrollView。 The ImageViews are programmatically inserted at runtime based on how many images are saved by the user previously. ImageViews 是在运行时根据用户之前保存的图像数量以编程方式插入的。 I get the error below:我收到以下错误:

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;试试这个: (1) 查看每个约束并尝试找出您不期望的; (2) find the code that added the unwanted constraint or constraints and fix it. (2) 找到添加不需要的约束或约束的代码并修复它。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) (注意:如果您看到 NSAutoresizingMaskLayoutConstraints 不明白,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

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

I do the basic Autolayout thing where the scrollview is pinned to all four sides at 0 points.我做了基本的 Autolayout 事情,其中​​滚动视图固定在 0 点处的所有四个边。 I then add a contentView as a subview (plain UIView) of the UIScrollView which is also pinned to all four sides at 0 points.然后我添加一个 contentView 作为 UIScrollView 的子视图(普通 UIView),它也固定到所有四个边的 0 点。

EDIT Storyboard constraints image编辑故事板约束图像

UIScrollview 上的故事板约束在所有方面都设置为 0 到超级视图,将后代约束设置为 0 到 contentView

I give the contentView a width in code like so:我在代码中给 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];

The UIImageViews are added like so where UIImageViews are added based on the number of pages set when the segue to the ViewController occurs.添加 UIImageViews 就像这样添加 UIImageViews 是基于在 ViewController 发生 segue 时设置的页数。

-(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];
    }
}

Additionally, when I scroll the UIScrollView the images displayed change size erratically on rotation.此外,当我滚动 UIScrollView 时,显示的图像在旋转时会不规则地改变大小。 I think that this is just a consequence of the above warning and the fact that I haven't layed out the UIImageViews yet.我认为这只是上述警告的结果以及我还没有布置 UIImageViews 的事实。 What does the above warning mean in this context and how do I fix it?在这种情况下,上述警告是什么意思,我该如何解决?

It seems you have pinned trailingMargin of scrollView to contentView.trailing.您似乎已将 scrollView 的 trailingMargin 固定到 contentView.trailing。 Change scrollView.Trailing Margin to scrollView.Trailing for this constraint You can do this in the activity inspector in the storyboard after selecting your constraint.将此约束的 scrollView.Trailing Margin 更改为 scrollView.Trailing 选择约束后,您可以在故事板的活动检查器中执行此操作。

Alternatively, clear all constraints on your contentView.或者,清除 contentView 上的所有约束。 Then while adding pinning constraints again uncheck Constrain to margins and set all constants 0.然后在再次添加固定约束时取消选中Constrain to margins并将所有常量设置为 0。

AND

Change this line in your code更改代码中的这一行

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

with this:有了这个:

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

Using @"H:|-[contentView(width)]-|"使用@"H:|-[contentView(width)]-|" in visual format means pinning your contentView to the superView's margins and adds a space of 8 pts between the superView and subView.在视觉格式中意味着将您的 contentView 固定到 superView 的边距并在 superView 和 subView 之间添加 8 pts 的空间。 In your storyboard constraints you had set up constraints with the Trailing and Leading edges of the UIScrollView, while in the programmatically added constraint you had used Trailing Margin and Leading Margin (kind of asking the contentView to maintain an 8 pt. padding).在您的故事板约束中,您使用 UIScrollView 的尾随和前导边缘设置了约束,而在以编程方式添加的约束中,您使用了尾随边距和前导边距(有点要求 contentView 保持 8 pt. 填充)。 Hence, the conflict.因此,冲突。

Check Visual Format syntax here . 在此处检查 Visual Format 语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM