【问题标题】:Auto layout error Unable to simultaneously satisfy constraints with UIScrollView自动布局错误无法同时满足 UIScrollView 的约束
【发布时间】:2015-12-20 05:15:20
【问题描述】:

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

无法同时满足约束。

可能至少以下列表中的一个约束是一个 你不想要。试试这个:(1)查看每个约束并尝试 找出你没想到的; (2) 找到添加的代码 不需要的约束或约束并修复它。 (注:如果你看到 NSAutoresizingMaskLayoutConstraints 你不明白的,参考 到 UIView 属性的文档 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 的 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 的事实。在这种情况下,上述警告是什么意思,我该如何解决?

【问题讨论】:

  • 您添加了哪些约束来将 contentView 固定到 scrollView?
  • ContentView 在 0 处固定到所有 4 侧的滚动视图。因此 contentView 的左侧在 0 点处固定到左侧滚动视图,对于 UIScrollView 的相应侧的 contentView 相同。 contentView 和 UIScrollView 之间没有其他约束

标签: ios uiscrollview autolayout visual-format-language


【解决方案1】:

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

或者,清除 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 的空间。在您的故事板约束中,您使用 UIScrollView 的 Trailing 和leading 边缘设置了约束,而在以编程方式添加的约束中,您使用了 Trailing Margin 和 Leading Margin(有点要求 contentView 保持 8 pt. padding)。因此,冲突。

检查视觉格式语法here

【讨论】:

  • 你能在 Storyboard 中看到带有约束的更新吗?我不确定尾随边距,因为它似乎被固定到超级视图。也许我没有正确理解?
  • 这是 scrollView 还是 contentView ?另外,您在固定 contentView 时是否取消选中“限制到边距”?
  • ScrollView 是的,我确实取消选中“限制到边距”
  • 太好了,谢谢。我已经接受了。您能否解释一下您建议的更改对发现相同问题的未来用户有何影响?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多