Xcode显示冲突约束

时间:2019-04-11 12:03:29

标签: swift constraints

我有一个主要由2个视图组成的视图控制器。 一个具有前导,尾部和底部锚点与superview对齐并与superview成比例的高度(0.25),以及一个滚动视图,将前导顶部和尾部与superview / safe区域和​​底部对齐,另一视图。

我在xib文件中定义了一个视图,该视图使用Bundle.main.loadNibNamed("VariantResultSlide", owner: self, options: nil)?.first多次创建,并将它们添加到数组slides中。我想将它们添加到ScrollView中:

for i in 0 ..< slides.count {
    scrollView.addSubview(slides[i])

    NSLayoutConstraint.activate([
        slides[i].leadingAnchor.constraint(equalTo: (i==0) ? scrollView.leadingAnchor : slides[i-1].trailingAnchor),
        slides[i].topAnchor.constraint(equalTo: scrollView.topAnchor),
        slides[i].bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
        slides[i].widthAnchor.constraint(equalTo: scrollView.widthAnchor),
        slides[i].heightAnchor.constraint(equalTo: scrollView.heightAnchor)
    ])
    if(i==slides.count-1) {
        NSLayoutConstraint.activate([slides[i].trailingAnchor.constraint(equalTo: scrollView.trailingAnchor)])
    }
    self.updateResultSlides(index: i, vehicle: orderedVehiclesList[i])
}

但是Xcode给了我类似的错误:

2019-04-11 13:57:07.219263+0200 FleetView[545:190663] [LayoutConstraints] 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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x282cecbe0 h=-&- v=-&- FleetView.VariantResultSlide:0x107f214a0.height == UIScrollView:0x102919200.height + 99   (active)>",
    "<NSLayoutConstraint:0x282c92300 FleetView.VariantResultSlide:0x107f214a0.height == UIScrollView:0x102919200.height   (active)>"
)

,幻灯片太大了。 但是我找不到任何为它们设置其他约束的地方。为什么会这样?

1 个答案:

答案 0 :(得分:0)

您的视图实现了topbottomheight约束,因此很明显,它的超级视图将需要调整大小-但由于自动调整大小蒙版自动创建的约束,因此它不能。使用

在您的超级视图中禁用它
yourViewsSuperview.translatesAutoresizingMaskIntoConstraints = false