Search code examples
iosswiftautolayoutuistackviewsnapkit

How to arrange views in the `UIStackView`?


I want to set four views in the UIStackView with the horizontal direction. The four views next to each without space.

enter image description here

My code:

let arrangeStackView = UIStackView()
let followUpActionView = UIView()
let developCurriculumView = UIView()
let moreMoreView = UIView()
let takeCareSalonView = UIView()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(arrangeStackView)

    developCurriculumView.translatesAutoresizingMaskIntoConstraints = false;
    followUpActionView.translatesAutoresizingMaskIntoConstraints = false;
    takeCareSalonView.translatesAutoresizingMaskIntoConstraints = false;
    moreMoreView.translatesAutoresizingMaskIntoConstraints = false;

    developCurriculumView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    followUpActionView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    takeCareSalonView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)
    moreMoreView.backgroundColor = UIColor(red: CGFloat(arc4random()%256)/255.0, green: CGFloat(arc4random()%256)/255.0, blue: CGFloat(arc4random()%256)/255.0, alpha: 1)


    arrangeStackView.axis = .horizontal
    arrangeStackView.distribution = .fillEqually

    arrangeStackView.spacing = 10
    arrangeStackView.alignment = .fill

    arrangeStackView.translatesAutoresizingMaskIntoConstraints = false
    arrangeStackView.addSubview(followUpActionView)
    arrangeStackView.addSubview(developCurriculumView)
    arrangeStackView.addSubview(moreMoreView)
    arrangeStackView.addSubview(takeCareSalonView)


    arrangeStackView.snp.makeConstraints { (make) in
        make.center.equalToSuperview()
        make.height.equalTo(95)
        make.width.equalToSuperview()
    }

    let yaIconWith = self.view.frame.width * 0.25

    followUpActionView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }
    takeCareSalonView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }
    moreMoreView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }
    developCurriculumView.snp.makeConstraints{(make) -> Void in
        make.height.equalToSuperview()
        make.width.equalTo(yaIconWith)
    }

Now, I look the result as below.

enter image description here

Why the four views locate the top-left on the screen?


Solution

  • You added each of the views as a subview to the stack view, instead of adding each view as an arranged subview. While they sound similar, only the arranged subviews are laid out according to the stack view's properties.

    So you will change these lines:

    arrangeStackView.addSubview(followUpActionView)
    arrangeStackView.addSubview(developCurriculumView)
    arrangeStackView.addSubview(moreMoreView)
    arrangeStackView.addSubview(takeCareSalonView)
    

    To this:

    arrangeStackView.addArrangedSubview(followUpActionView)
    arrangeStackView.addArrangedSubview(developCurriculumView)
    arrangeStackView.addArrangedSubview(moreMoreView)
    arrangeStackView.addArrangedSubview(takeCareSalonView)
    

    And if you need special arrangement or if you need to add the views at different times in certain places, insertArrangedSubview(_:at:) is an alternative to addArrangedSubview(_:)