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

标签 ios uiscrollview autolayout visual-format-language

我有一个用于显示 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>"

我做了基本的 Autolayout 事情, ScrollView 在 0 点固定到所有四个边。然后我添加一个 contentView 作为 UIScrollView 的 subview (纯 UIView),它也固定在所有四个边的 0 点。

编辑 Storyboard约束图像

storyboard constraints on UIScrollview set to 0 on all sides to superview with Descendent constraints set to 0 to contentView

我在代码中给 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 发生 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];
    }
}

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

最佳答案

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

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

更改代码中的这一行

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 的空间。在您的 Storyboard 约束中,您已经使用 UIScrollView 的后缘和前缘设置了约束,而在以编程方式添加的约束中,您使用了尾随边距和前导边距(有点要求 contentView 保持 8 磅的填充)。因此,冲突。

检查视觉格式语法 here .

关于ios - 自动布局错误 Unable to simultaneously satisfy constraints with UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32730218/

相关文章:

ios - 如何找出坐标之间的距离?

iphone - 在 Objective-C 中,在每个类中导入相同的 header 会使编译时间变长?

iphone - 自动调整 UITextView 和 UIScrollView 的大小

ios - Xcode 7 自动布局是否损坏或难以使用

ios - 在代码中为元素的动态数量设置约束

iOS 自动布局约束和框架不起作用

ios - 无法从 NSDictionary iOS 获取所有匹配字符串

ios - "Convenience initializer missing a ' self ' call to another initializer"

ios - 即使在 tableView reloadData 之后 UITableView 选择也会突出显示

ios - 设置 UIScrollView 宽度