spa0201su
spa0201su

Reputation: 125

Unable to simultaneously satisfy constraints. I'm not sure which code causes the conflict

I have two views. and they were created in storyboard and have outlet variables. (topView and bottomSecondview) When I turn on switchState, I'd like secondBottomView to show up and when I turn on switchstate2, I'd like topView to show up. it works well until I turn on the both switches. once I turn on the both switches (which means the both views have to show up), I see this in console: Unable to simultaneously satisfy constraints. I'm not sure where there's the conflict. Please help me.

    if UserDefaults.standard.bool(forKey:"switchState") == true && UserDefaults.standard.bool(forKey: "switchState2") == false {
         topView.isHidden = true
        secondBottomView.isHidden = false
             secondBottomView.translatesAutoresizingMaskIntoConstraints = false
             secondBottomView.topAnchor.constraint(equalTo: self.myUIProgressView.bottomAnchor, constant: 11).isActive = true
             secondBottomView.leadingAnchor.constraint(equalTo: self.myUIProgressView.leadingAnchor, constant: 0).isActive = true
             secondBottomView.bottomAnchor.constraint(equalTo: self.bottomView.topAnchor, constant: -11).isActive = true
             secondBottomView.trailingAnchor.constraint(equalTo: self.myUIProgressView.trailingAnchor, constant: 0).isActive = true          
    }

    if UserDefaults.standard.bool(forKey: "switchState2")  == true && UserDefaults.standard.bool(forKey: "switchState") == false {
            secondBottomView.isHidden = true
           topView.isHidden = false
         topView.translatesAutoresizingMaskIntoConstraints = false
         topView.topAnchor.constraint(equalTo: myUIProgressView.bottomAnchor, constant: 111).isActive = true
         topView.bottomAnchor.constraint(equalTo: bottomView.topAnchor , constant: -11).isActive = true
         topView.leadingAnchor.constraint(equalTo: myUIProgressView.leadingAnchor, constant: 0).isActive = true
         topView.trailingAnchor.constraint(equalTo: myUIProgressView.trailingAnchor, constant: 0).isActive = true
                   }

if UserDefaults.standard.bool(forKey: "switchState") == true && UserDefaults.standard.bool(forKey: "switchState2") == true {
        topView.isHidden = false
        topView.translatesAutoresizingMaskIntoConstraints = false
        topView.topAnchor.constraint(equalTo: myUIProgressView.bottomAnchor, constant: 10).isActive = true
        topView.bottomAnchor.constraint(equalTo: secondBottomView.topAnchor, constant: -10).isActive = true
        topView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true
        topView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10).isActive = true
        topView.widthAnchor.constraint(equalTo: secondBottomView.widthAnchor, multiplier: 1).isActive = true
        topView.heightAnchor.constraint(equalTo: secondBottomView.heightAnchor, multiplier: 1).isActive = true

        secondBottomView.isHidden = false
        secondBottomView.translatesAutoresizingMaskIntoConstraints = false
        secondBottomView.bottomAnchor.constraint(equalTo: bottomView.topAnchor, constant:-10).isActive = true
        secondBottomView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true
        secondBottomView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10).isActive = true
        secondBottomView.widthAnchor.constraint(equalTo: topView.widthAnchor, multiplier: 1).isActive = true
        secondBottomView.heightAnchor.constraint(equalTo: topView.heightAnchor, multiplier: 1).isActive = true
      secondBottomView.topAnchor.constraint(equalTo: self.myUIProgressView.bottomAnchor, constant: 11).isActive = false       
    }

Upvotes: 0

Views: 55

Answers (1)

sats
sats

Reputation: 657

Both topView and secondBottomView has the topAnchor set to the bottomAnchor of progressView. Remove the topAnchor constraint on secondBottomView.

There seems to be quite a few problems with your constraints. It's easier for you to achieve something similar using UIStackView with auto layout. Add a new UIStackView with vertical axis under your progressView. Add both topView and secondBottomView to the UIStackView. On changing the switch states, simple show or hide the topView or secondBottomView as

topView.isHidden = true; // Hides topView
secondBottomView.isHidden = true; // Hides secondBottomView

This way, using UIStackView, you wouldn't have to take care of all those constraints.

Upvotes: 2

Related Questions