通过数组

时间:2018-03-12 10:31:35

标签: ios swift swift4 nslayoutconstraint uistackview

我有一个使用以下代码

从Web服务创建的StackViews数组
if !self.eventFields.isEmpty {
var i:Int = 0
for field in self.eventFields {
    if field.ControlName == "fraBackground" || field.ControlName == "picBottom" {
    } else {

        var stackedInfoView: UIStackView!

        let captionField: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.font = Style.associateMainText
            label.textColor = .darkText
            label.numberOfLines = 0
            label.sizeToFit()
            label.text = field.Caption
            return label
        }()

        let dataField: UILabel = {
            let label = UILabel()
            label.translatesAutoresizingMaskIntoConstraints = false
            label.font = Style.associateSubText
            label.textColor = .darkText
            label.numberOfLines = 0
            label.text = "Lots of text"
            label.sizeToFit()
            return label
        }()

        stackedInfoView = UIStackView(arrangedSubviews: [captionField, dataField])
        stackedInfoView.axis = .horizontal
        stackedInfoView.distribution = .equalSpacing
        stackedInfoView.alignment = .center
        stackedInfoView.spacing = 30.0
        stackedInfoView.tag = i
        stackedInfoView.translatesAutoresizingMaskIntoConstraints = false

        self.viewArray.append(stackedInfoView)

        i = i + 1

    }
}
self.layoutSubViews()
}

当我布置子视图时,我可以添加视图,但是它们都相互叠加,因为我无法弄清楚如何添加约束来观察前一个视图的底部锚点。我该如何实现这一目标? - 注意,在堆栈视图之前有一定数量的固定视图,所以下面的代码添加堆栈视图并将它们展开可能看起来不必要地复杂,但它是必需的(除非有人可以建议一个更简洁的方法来做它?)< / p>

if !self.viewArray.isEmpty {
for dynamicView in self.viewArray {
    view.addSubview(dynamicView)
    if view.tag == 0 {
        dynamicView.anchor(eventSummaryLabel.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 10, leftConstant: 20, bottomConstant: 0, rightConstant: 20, widthConstant: 0, heightConstant: 0)
    } else {
        dynamicView.anchor(dynamicView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 10, leftConstant: 20, bottomConstant: 0, rightConstant: 20, widthConstant: 0, heightConstant: 0)
    }
}
}

1 个答案:

答案 0 :(得分:2)

我不确定你在这里问的是什么。一些结果演示可能会有所帮助......

但似乎您正在研究如何以编程方式设置约束以创建类似布局的列表。要对垂直(例如)执行此操作,应执行以下操作:

func verticallyLayoutViews(_ views: [UIView], in parent: UIView) {

    var previousView: UIView?

    views.forEach { view in
        view.translatesAutoresizingMaskIntoConstraints = false
        parent.addSubview(view)
        if let previousView = previousView {
            parent.addConstraint(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: previousView, attribute: .bottom, multiplier: 1.0, constant: 0.0))
        }
        parent.addConstraint(NSLayoutConstraint(item: view, attribute: .left, relatedBy: .equal, toItem: parent, attribute: .left, multiplier: 1.0, constant: 0.0))
        parent.addConstraint(NSLayoutConstraint(item: view, attribute: .right, relatedBy: .equal, toItem: parent, attribute: .right, multiplier: 1.0, constant: 0.0))

        previousView = view
    }

    // Set first constraint
    if let first = views.first {
       parent.addConstraint(NSLayoutConstraint(item: first, attribute: .top, relatedBy: .equal, toItem: parent, attribute: .top, multiplier: 1.0, constant: 0.0))
        // Set last constraint
        if let last = previousView {
            parent.addConstraint(NSLayoutConstraint(item: last, attribute: .bottom, relatedBy: .equal, toItem: parent, attribute: .bottom, multiplier: 1.0, constant: 0.0))
        }
    }


}