Search code examples
swiftuikitxcode11uistackviewios14

Elements not adding to UIStackView , programmatically?


I was trying to make a sorting Visualizer in Live playground so I created a stack view but when I try to add UIView (as the sticks in that visualiser) they do not get added in my stackView , Below is my code It is big but your are interested in mainStackview part only because that is the stackView where I wanna add that sticks(UIView), Also buildStartingArray() function (it is used to add the UIViews)

public class HomeViewController:UIViewController{

    let stackView:UIStackView = {
       let st = UIStackView()
        st.axis = .horizontal
        st.alignment = .center
        st.distribution = .fill
//        st.backgroundColor = .cyan
        st.layer.shadowColor = UIColor.gray.cgColor
        st.layer.shadowOffset = .zero
        st.layer.shadowRadius = 5
        st.layer.shadowOpacity = 1
        st.spacing = 10
        st.translatesAutoresizingMaskIntoConstraints = false
        
       return st
    }()
    let generateButton:UIButton = {
       let btn = UIButton()
        btn.setTitle("Generate Array", for: .normal)
        btn.backgroundColor = UIColor(red: 0.92, green: 0.30, blue: 0.29, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    let BubbleSort:UIButton = {
       let btn = UIButton()
        btn.setTitle("BubbleSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.41, green: 0.43, blue: 0.88, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    let MergeSort:UIButton = {
       let btn = UIButton()
        btn.setTitle("MergeSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.10, green: 0.16, blue: 0.34, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    let InsertionSort:UIButton = {
       let btn = UIButton()
        btn.setTitle("InsertionSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.19, green: 0.22, blue: 0.32, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    let SelectionSort:UIButton = {
       let btn = UIButton()
        btn.setTitle("SelectionSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.51, green: 0.20, blue: 0.44, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    }()
    let mainStackView:UIStackView = {
        let st = UIStackView()
        st.backgroundColor = .gray
        st.axis = .horizontal
        st.distribution = .fillEqually
        st.alignment = .firstBaseline
        st.spacing = 1
        st.translatesAutoresizingMaskIntoConstraints = false
        return st
    }()
    let baseView:UIView = {
        let vw = UIView()
        vw.backgroundColor = UIColor(red: 0.07, green: 0.54, blue: 0.65, alpha: 1.00)
        vw.translatesAutoresizingMaskIntoConstraints = false
        vw.layer.cornerRadius = 3
        vw.layer.masksToBounds = true
        vw.heightAnchor.constraint(equalToConstant: 15).isActive = true
        return vw
    }()
    public override   func viewDidLoad() {
          view.addSubview(mainStackView)
          view.addSubview(stackView)
          view.addSubview(baseView)
        edgesForExtendedLayout = []
        stackView.addArrangedSubview(generateButton)
        stackView.addArrangedSubview(BubbleSort)
        stackView.addArrangedSubview(MergeSort)
        stackView.addArrangedSubview(InsertionSort)
        stackView.addArrangedSubview(SelectionSort)
        
        stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        stackView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        stackView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        stackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
        
        baseView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -2).isActive = true
        baseView.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 5).isActive = true
        baseView.rightAnchor.constraint(equalTo: view.rightAnchor,constant: -5).isActive = true

         mainStackView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 5).isActive = true
        mainStackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 5).isActive = true
        mainStackView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -5).isActive = true
        mainStackView.bottomAnchor.constraint(equalTo: baseView.topAnchor, constant: -5).isActive = true
        setButtons()
        builStartingArray()

    }

   func setButtons(){
        generateButton.addTarget(self, action: #selector(generatePressed), for: .touchUpInside)
        BubbleSort.addTarget(self, action: #selector(bubbleSort), for: .touchUpInside)
        MergeSort.addTarget(self, action: #selector(mergeSort), for: .touchUpInside)
        InsertionSort.addTarget(self, action: #selector(insertionSort), for: .touchUpInside)
        SelectionSort.addTarget(self, action: #selector(selectionSort), for: .touchUpInside)
    }
    func builStartingArray(){
        let viewStick:UIView = {
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.frame.size = CGSize(width: 50, height: 150)
            return v
        }()
        let viewStick2:UIView = {
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.frame.size = CGSize(width: 50, height: 150)
            return v
        }()
        mainStackView.addArrangedSubview(viewStick)
        mainStackView.addArrangedSubview(viewStick2)
    }
}

Problem is the UIView that I add in buildStartingArray() function doesn't show up ,And for now I have added only 2 view but in future I am planning to add 30-35 in mainStackView,

I want those UIViews to look like graph bars and the mainStackView is my graph

Here is the current output Output the mainStackView is the one with gray background


Solution

  • Make main stackView to align bottom

    You can try to adding a height for mainStack subviews like

    v.translatesAutoresizingMaskIntoConstraints = false
    v.heightAnchor.constraint(equalToConstant: 150).isActive = true
    

    in

    func builStartingArray(){
        let viewStick:UIView = {
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.heightAnchor.constraint(equalToConstant: 150).isActive = true
            return v
        }()
        let viewStick2:UIView = {
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.heightAnchor.constraint(equalToConstant: 150).isActive = true
            return v
        }()
        mainStackView.addArrangedSubview(viewStick)
        mainStackView.addArrangedSubview(viewStick2)
    }
    

    All code

    import UIKit
    
    class ViewController:UIViewController{
    
       let stackView:UIStackView = {
           let st = UIStackView()
            st.axis = .horizontal
            st.alignment = .center
            st.distribution = .fill
    //        st.backgroundColor = .cyan
            st.layer.shadowColor = UIColor.gray.cgColor
            st.layer.shadowOffset = .zero
            st.layer.shadowRadius = 5
            st.layer.shadowOpacity = 1
            st.spacing = 10
            st.translatesAutoresizingMaskIntoConstraints = false
            
           return st
        }()
        let generateButton:UIButton = {
           let btn = UIButton()
            btn.setTitle("Generate Array", for: .normal)
            btn.backgroundColor = UIColor(red: 0.92, green: 0.30, blue: 0.29, alpha: 1.00)
            btn.setTitleColor(.white, for: .normal)
            btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
            btn.layer.cornerRadius = 10
            btn.layer.masksToBounds = true
            btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
            btn.translatesAutoresizingMaskIntoConstraints = false
            return btn
        }()
        let BubbleSort:UIButton = {
           let btn = UIButton()
            btn.setTitle("BubbleSort", for: .normal)
            btn.backgroundColor = UIColor(red: 0.41, green: 0.43, blue: 0.88, alpha: 1.00)
            btn.setTitleColor(.white, for: .normal)
            btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
            btn.layer.cornerRadius = 10
            btn.layer.masksToBounds = true
            btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
            btn.translatesAutoresizingMaskIntoConstraints = false
            return btn
        }()
        let MergeSort:UIButton = {
           let btn = UIButton()
            btn.setTitle("MergeSort", for: .normal)
            btn.backgroundColor = UIColor(red: 0.10, green: 0.16, blue: 0.34, alpha: 1.00)
            btn.setTitleColor(.white, for: .normal)
            btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
            btn.layer.cornerRadius = 10
            btn.layer.masksToBounds = true
            btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
            btn.translatesAutoresizingMaskIntoConstraints = false
            return btn
        }()
        let InsertionSort:UIButton = {
           let btn = UIButton()
            btn.setTitle("InsertionSort", for: .normal)
            btn.backgroundColor = UIColor(red: 0.19, green: 0.22, blue: 0.32, alpha: 1.00)
            btn.setTitleColor(.white, for: .normal)
            btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
            btn.layer.cornerRadius = 10
            btn.layer.masksToBounds = true
            btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
            btn.translatesAutoresizingMaskIntoConstraints = false
            return btn
        }()
        let SelectionSort:UIButton = {
           let btn = UIButton()
            btn.setTitle("SelectionSort", for: .normal)
            btn.backgroundColor = UIColor(red: 0.51, green: 0.20, blue: 0.44, alpha: 1.00)
            btn.setTitleColor(.white, for: .normal)
            btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
            btn.layer.cornerRadius = 10
            btn.layer.masksToBounds = true
            btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
            btn.translatesAutoresizingMaskIntoConstraints = false
            return btn
        }()
        let mainStackView:UIStackView = {
            let st = UIStackView()
            st.backgroundColor = .gray
            st.axis = .horizontal
            st.distribution = .fillEqually
            st.alignment = .top
            st.spacing = 1
            st.translatesAutoresizingMaskIntoConstraints = false
            return st
        }()
        let baseView:UIView = {
            let vw = UIView()
            vw.backgroundColor = UIColor(red: 0.07, green: 0.54, blue: 0.65, alpha: 1.00)
            vw.translatesAutoresizingMaskIntoConstraints = false
            vw.layer.cornerRadius = 3
            vw.layer.masksToBounds = true
            return vw
        }()
        public override   func viewDidLoad() {
              view.addSubview(mainStackView)
              view.addSubview(stackView)
              view.addSubview(baseView)
            edgesForExtendedLayout = []
            stackView.addArrangedSubview(generateButton)
            stackView.addArrangedSubview(BubbleSort)
            stackView.addArrangedSubview(MergeSort)
            stackView.addArrangedSubview(InsertionSort)
            stackView.addArrangedSubview(SelectionSort)
            
            stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            stackView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            stackView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            stackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
            
            baseView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -2).isActive = true
            baseView.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 5).isActive = true
            baseView.rightAnchor.constraint(equalTo: view.rightAnchor,constant: -5).isActive = true
            baseView.heightAnchor.constraint(equalToConstant: 15).isActive = true
            
            mainStackView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 5).isActive = true
            mainStackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 5).isActive = true
            mainStackView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -5).isActive = true
            mainStackView.bottomAnchor.constraint(equalTo: baseView.topAnchor, constant: -5).isActive = true
     
            builStartingArray()
    
        }
     
        func builStartingArray(){
            let viewStick:UIView = {
               let v = UIView()
                v.backgroundColor = .red
                v.translatesAutoresizingMaskIntoConstraints = false
                v.heightAnchor.constraint(equalToConstant: 150).isActive = true
                return v
            }()
            let viewStick2:UIView = {
               let v = UIView()
                v.backgroundColor = .red
                v.translatesAutoresizingMaskIntoConstraints = false
                v.heightAnchor.constraint(equalToConstant: 150).isActive = true
                return v
            }()
            mainStackView.addArrangedSubview(viewStick)
            mainStackView.addArrangedSubview(viewStick2)
        }
    }
    

    enter image description here