简体   繁体   中英

Xcode shows conflicting constraints

I have a view controller consisting mainly of 2 views. 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.

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 . I want to add them to my 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:

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. Disable it in you superview with

yourViewsSuperview.translatesAutoresizingMaskIntoConstraints = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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