首页 > 解决方案 > 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)
              ])
        
        
        }

标签: swiftfunctionautolayoutconstraintsuistackview

解决方案


好吧,您正在设置顶部底部约束以stackview使其具有一定的高度,然后您还为按钮设置了高度约束。这将强制堆栈视图具有一定的大小,如果不可能,它将导致您看到的错误。要从中恢复,操作系统将不得不删除您的冲突约束之一,在您的情况下是按钮的高度。删除您的高度约束,PushUpButton您将对问题进行排序。让stackview完成它的工作并放置其排列的子视图。


推荐阅读