代码之家  ›  专栏  ›  技术社区  ›  Adrian

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

  •  1
  • Adrian  · 技术社区  · 6 年前

    这是我想要实现的布局

    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

    它应该出现在“生产力图表”标题下,但它出现在单元格内容视图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 回复  |  直到 6 年前
        1
  •  2
  •   bseh    6 年前

    向uistackview添加新的子视图时,应该使用 addArrangedSubview 方法。

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