user14469311
user14469311

Reputation:

Problems with StackView after using a function the second time

**Hey, I have a function which brings me to the next VC and everything is fine, but when I'm using the function Button the second time, the next VC is just a white view... and this appears in the console:

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x2808b4140 'UISV-canvas-connection' UIStackView:0x10540d410.top == UIButton:0x10540e1b0'0'.top (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful. 2020-11-09 05:44:46.315025+0100 PushUps+[4013:295478] [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. ( "<NSLayoutConstraint:0x2808ba4e0 UIStackView:0x10540d410.bottom == UIView:0x105304b40.bottom - 50 (active)>", "<NSLayoutConstraint:0x2808ba800 UIButton:0x10540e1b0'0'.centerY == UIView:0x105304b40.centerY (active)>", "<NSLayoutConstraint:0x2808ba850 UIButton:0x10540e1b0'0'.height == 500 (active)>", "<NSLayoutConstraint:0x2808b4000 'UISV-canvas-connection' V:[UIButton:0x10540e1b0'0']-(0)-| (active, names: '|':UIStackView:0x10540d410 )>", "<NSLayoutConstraint:0x280884960 'UIView-Encapsulated-Layout-Height' UIView:0x105304b40.height == 896
(active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x2808ba850 UIButton:0x10540e1b0'0'.height == 500 (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

What does this mean? When I'm removing the all the constraints besides of the hight/widht constraints the VC is completely displaced so I need all the x,y,top and bottom anchors

Code of the StackView:

class PushUpViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        activateProximitySensor()
        startTimer()
        view.backgroundColor = .white
        setUpStackView()
    }
    
        func setUpStackView() {
        // SetUp StackView:
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical
        stackView.alignment = .center
        stackView.distribution = .fillEqually
        stackView.spacing = 40
        view.addSubview(stackView)
        
        // SetUp StackView Constraints:
            stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true
               stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
               stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
//        stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        
        // Set Elements to StackView:
        stackView.addArrangedSubview(TimeLabel)
        stackView.addArrangedSubview(PushUpButton)
        

   // SetUp PushUpButton:
       PushUpButton.backgroundColor = .white
       PushUpButton.setTitle("\(count)", for: .normal)
       PushUpButton.setTitleColor(.systemGray, for: .normal)
       PushUpButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 70)
       
       
       PushUpButton.translatesAutoresizingMaskIntoConstraints = false
       
       PushUpButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
       PushUpButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
       PushUpButton.heightAnchor.constraint(equalToConstant: 500).isActive = true
       PushUpButton.widthAnchor.constraint(equalToConstant: 400).isActive = true
       
   
// SetUp TimeLabel
       TimeLabel.textAlignment = .center
               TimeLabel.text = "\(counter)"
               TimeLabel.textColor = .black
               TimeLabel.font = .boldSystemFont(ofSize: 30)
               self.view.addSubview(TimeLabel)
               
               TimeLabel.translatesAutoresizingMaskIntoConstraints = false
               
    NSLayoutConstraint.activate([
               TimeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
               TimeLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
               TimeLabel.widthAnchor.constraint(equalToConstant: 200),
               TimeLabel.heightAnchor.constraint(equalToConstant: 200)
    
    ])
        
        
        // SetUp SecondStackView
        secondStackView.translatesAutoresizingMaskIntoConstraints = false
        secondStackView.axis = .horizontal
        secondStackView.alignment = .center
        secondStackView.distribution = .fillEqually
        view.addSubview(secondStackView)
        
        // SetUp SecondStackView Constrains
        secondStackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        secondStackView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 10).isActive = true
        
        
        // Set Elements:
        secondStackView.addArrangedSubview(breakButton)
        secondStackView.addArrangedSubview(stopbutton)
        
        //SetUp BreakButton
              breakButton.backgroundColor = .lightGray
              breakButton.setTitle("Break", for: .normal)
              breakButton.setTitle("Start", for: .selected)
        breakButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
              breakButton.setTitleColor(.white, for: .normal)
              breakButton.layer.cornerRadius = 12
              breakButton.layer.borderWidth = 1
              breakButton.layer.borderColor = UIColor.white.cgColor
           breakButton.addTarget(self, action: #selector(BreakButtonTapped), for: .touchUpInside)
              view.addSubview(breakButton)
              
              breakButton.translatesAutoresizingMaskIntoConstraints = false
              
           NSLayoutConstraint.activate([
              breakButton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -100),
              breakButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30),
              breakButton.widthAnchor.constraint(equalToConstant: 150),
              breakButton.heightAnchor.constraint(equalToConstant: 50)
           ])
          
            
           
          
          // SetUp StopButton:
              stopbutton.backgroundColor = .systemRed
              stopbutton.setTitle("Stop", for: .normal)
              stopbutton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
              stopbutton.setTitleColor(.white, for: .normal)
              stopbutton.layer.cornerRadius = 12
              stopbutton.layer.borderWidth = 1
              stopbutton.layer.borderColor = UIColor.white.cgColor
           stopbutton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
              view.addSubview(stopbutton)
              
              stopbutton.translatesAutoresizingMaskIntoConstraints = false
              
           NSLayoutConstraint.activate([
              stopbutton.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 100),
              stopbutton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30),
              stopbutton.widthAnchor.constraint(equalToConstant: 150),
              stopbutton.heightAnchor.constraint(equalToConstant: 50)
              ])
        
        
        }

Upvotes: 0

Views: 376

Answers (2)

Pancho
Pancho

Reputation: 4143

Well you're setting top and bottom constraints to your stackview making it of certain height, and then you're also setting a height constraint to your button. This will force the stackview to be of a certain size, and if not possible it will result in the error you're seeing. To recover from this the OS will have to remove one of your conflicting constraints, which in your case is the height of your button. Remove the heigh constraint on your PushUpButton and you'll sort the issue. Let the stackview do its job and place its arranged subviews.

Upvotes: 0

zeytin
zeytin

Reputation: 5643

After adding stackView as subview , add as below. Seems like missing.

stackView.translatesAutoresizingMaskIntoConstraints = false

Upvotes: 0

Related Questions