2

This is the layout I want to achieve

Label1 UIView Label2 <- horizontally stack view
Label1 UIView Label2
Label1 UIView Label2
Label1 UIView Label2
Label1 UIView Label2

Every horizontally stack view contains a label and view and another label. After that the horizontal stack view is added to a vertical one.

So all of that layout is a vertical stack view.

I want to achieve this inside a UITableViewCell. Here is my code:

let verticalStackView: UIStackView = {
        let hsv = UIStackView()
        hsv.axis = .vertical
        hsv.alignment = .fill
        hsv.distribution = .fill
        hsv.spacing = 10
        hsv.translatesAutoresizingMaskIntoConstraints = false

        return hsv
    }()

    override func awakeFromNib() {
        super.awakeFromNib()

        for i in 1..<10 {
            let dayLbl: UILabel = {
                let l = UILabel()
                l.text = "Day " + String(i)
                l.font = UIFont.preferredFont(forTextStyle: .caption1)
                l.translatesAutoresizingMaskIntoConstraints = false

                return l;
            }()

            let progressBar: ProgressBar = {
               let pb = ProgressBar(frame: CGRect(x: 0, y: 0, width: 200, height: 12))
                pb.translatesAutoresizingMaskIntoConstraints = false

                return pb;
            }()

            let gradeLbl: UILabel = {
                let l = UILabel()
                l.text = String(i)
                l.font = UIFont.preferredFont(forTextStyle: .headline)
                l.translatesAutoresizingMaskIntoConstraints = false

                return l;
            }()

            let horizontalStackView: UIStackView = {
               let hsv = UIStackView()
                hsv.axis = .horizontal
                hsv.alignment = .fill
                hsv.distribution = .fill
                hsv.spacing = 5
                hsv.translatesAutoresizingMaskIntoConstraints = false

                return hsv
            }()

            horizontalStackView.addSubview(dayLbl)
            horizontalStackView.addSubview(progressBar)
            horizontalStackView.addSubview(gradeLbl)

            NSLayoutConstraint.activate([
                horizontalStackView.heightAnchor.constraint(equalToConstant: 12)
            ])

            verticalStackView.addSubview(horizontalStackView)
        }

        contentView.addSubview(verticalStackView)

        NSLayoutConstraint.activate([
            verticalStackView.topAnchor.constraint(equalTo: vcTitle.bottomAnchor, constant: 30),
            verticalStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
            verticalStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 20),
            verticalStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 30),
            ])
    }

This is what I get:

enter image description here

It was supposed to appear under the "Productivity Chart" title, but it appears on the cell content view x:0 y: 0 location. And there is only one line and everything looks crowded there.

Any idea what I am doing wrong ?

This is how one line should look:

Day 1 ----------------- 7   
(where ----- is the view).

edit:

After replacing addSubView with addArrangedSubview:

enter image description here

edit 2:

The right part truncated is fixed too. I changed:

verticalStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 20)

to

contentView.trailingAnchor.constraint(equalTo: verticalStackView.trailingAnchor, constant: 20)
4
  • @bseh: ohh I didn't know that. Yeah, now they appear under the title, but the right part is truncated also the last 3 rows. But it is a start Commented Jun 2, 2018 at 14:44
  • I updated the question with the new results. Now need to figure out why the right side is truncated and part of the bottom. Commented Jun 2, 2018 at 14:47
  • I'll add my comment as an answer then :) Commented Jun 2, 2018 at 14:51
  • @yep, please do. Commented Jun 2, 2018 at 14:51

1 Answer 1

2

When you are adding new subviews to a UIStackView, you should use addArrangedSubview method.

The stack view ensures that the arrangedSubviews array is always a subset of its subviews array. This method automatically adds the provided view as a subview of the stack view, if it is not already. If the view is already a subview, this operation does not alter the subview ordering.

Sign up to request clarification or add additional context in comments.

2 Comments

The link to a solution might not help as the ink might change in the future. It's better to add an answer here and reference to a link.
You are right, I added the description to my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.