简体   繁体   English

Xcode显示冲突约束

[英]Xcode shows conflicting constraints

I have a view controller consisting mainly of 2 views. 我有一个主要由2个视图组成的视图控制器。 One that has leading, trailing and bottom anchor aligned with superview and proportional height to superview(0.25), and a scroll view that aligns leading top and trailing to superview/safe area and bottom the other view. 一个具有前导,尾随和底锚与Superview和比例高度对齐的Superview(0.25),以及一个滚动视图,将前导顶和尾随与Superview /安全区域和底端对齐。

I have a view defined in a xib-file which I create multiple times using Bundle.main.loadNibNamed("VariantResultSlide", owner: self, options: nil)?.first and add them to an array slides . 我在xib文件中定义了一个视图,该视图使用Bundle.main.loadNibNamed("VariantResultSlide", owner: self, options: nil)?.first创建多次,并将它们添加到数组slides I want to add them to my ScrollView: 我想将它们添加到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])
}

But then Xcode gives me errors like: 但是随后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)>"
)

and the slides are way too big. 幻灯片太大了。 But I can't find any place where I set another constraint for them. 但是我找不到任何为它们设置其他约束的地方。 Why is this happening? 为什么会这样呢?

Your view implements top , bottom and height constraints so obviously its superview will need to resize - but it can't due to constraints created automatically from autoresizing mask. 您的视图实现了topbottomheight约束,因此很明显,需要调整其top视图的大小-但它不会由于自动调整蒙版自动创建的约束而导致。 Disable it in you superview with 在您的超级视图中将其禁用

yourViewsSuperview.translatesAutoresizingMaskIntoConstraints = false

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

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