以编程方式在垂直堆栈视图中添加多个水平堆栈视图

时间:2018-06-02 14:37:58

标签: ios swift uistackview

这是我想要实现的布局

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

每个水平堆栈视图都包含标签和视图以及另一个标签。 之后,水平堆栈视图将添加到垂直堆栈视图中。

因此,所有布局都是垂直堆栈视图。

我想在UITableViewCell内实现这一目标。这是我的代码:

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),
            ])
    }

这就是我得到的:

enter image description here

它应该出现在&#34; Productivity Chart&#34;标题,但它出现在单元格内容视图x:0 y:0位置。而且只有一条线,一切看起来都很拥挤。

知道我做错了吗?

这是一行的外观:

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

编辑:

addSubView替换addArrangedSubview后:

enter image description here

编辑2:

截断的右侧部分也是固定的。我改变了:

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

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

1 个答案:

答案 0 :(得分:2)

在向UIStackView添加新子视图时,应使用addArrangedSubview方法。

  

堆栈视图确保arrangeSubviews数组始终为a   其子视图数组的子集。此方法自动添加   提供视图作为堆栈视图的子视图(如果尚未提供)。如果   视图已经是子视图,此操作不会改变   子视图排序。