0

am now getting the following error

Expression resolves to an unused property with the following code

 func setlayout(){
    //containerStackView.translatesAutoresizingMaskIntoConstraints
    containerStackView.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive
    containerStackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive
    containerStackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive
}

On the following code

  func setlayout(){
            containerStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
            containerStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
           
            
        }

However the containerStackView is not showing at all.

My code:

    import UIKit
    class ViewController: UIViewController {
        
            /* V VIEWS */
            let containerStackView = UIStackView()
            
            let verticalStackView = UIStackView()
            let verticalViewOne = UIView()
            let verticalViewTwo = UIView()
            let verticalViewThree = UIView()
        
         override func viewDidLoad() {
                super.viewDidLoad()
                setupViews()
                setupHierachy()
                setlayout()
        }
        
            
            
            func setupViews(){
                verticalStackView.axis = .vertical
                verticalStackView.translatesAutoresizingMaskIntoConstraints = false
                verticalStackView.distribution = .fillEqually
                
                verticalViewOne.backgroundColor = .red
                
                containerStackView.axis = .vertical
                containerStackView.spacing = 15
                containerStackView.distribution = .fillEqually
            }
            
            func setupHierachy(){
        view.addSubview(containerStackView)
        view.addSubview(verticalStackView)
        
        verticalStackView.addArrangedSubview(verticalViewOne)
        containerStackView.addArrangedSubview(verticalStackView)
    }
    
   func setlayout(){
    //containerStackView.translatesAutoresizingMaskIntoConstraints
    containerStackView.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive
    containerStackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive
    containerStackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive
}
        }
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204

1 Answers1

0

you are missing active = true , in the set setlayout() , also you have to add bottom or height constraint to containerStackView, also I recommend you check the tutorials for this YouTube channel https://www.youtube.com/channel/UCuP2vJ6kRutQBfRmdcI92mA this guy teach about create layout only using code.

import UIKit


    class ViewController: UIViewController {
        
            /* V VIEWS */
            let containerStackView = UIStackView()
            
            let verticalStackView = UIStackView()
            let verticalViewOne = UIView()
            let verticalViewTwo = UIView()
            let verticalViewThree = UIView()
        
         override func viewDidLoad() {
                super.viewDidLoad()
                setupViews()
                setupHierachy()
                setlayout()
        }
        
            
            
            func setupViews(){
                verticalStackView.axis = .vertical
                verticalStackView.translatesAutoresizingMaskIntoConstraints = false
                verticalStackView.distribution = .fillEqually
                containerStackView.translatesAutoresizingMaskIntoConstraints = false
                verticalViewOne.backgroundColor = .red
                
                containerStackView.axis = .vertical
                containerStackView.spacing = 15
                containerStackView.distribution = .fillEqually
            }
            
            func setupHierachy(){
        view.addSubview(containerStackView)
        view.addSubview(verticalStackView)
        
        verticalStackView.addArrangedSubview(verticalViewOne)
        containerStackView.addArrangedSubview(verticalStackView)
    }
    
   func setlayout(){
    //containerStackView.translatesAutoresizingMaskIntoConstraints
    containerStackView.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
    containerStackView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    containerStackView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    containerStackView.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}
        }

cristian_064
  • 576
  • 3
  • 16