通过数组添加的视图的约束
Constraints for views added via array
我有一个 StackViews 数组,它是使用以下代码从 Web 服务创建的
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()
}
当我布局子视图时,我可以很好地添加视图,但是它们都相互重叠,因为我不知道如何添加约束来观察前一个视图的底部锚点。我如何实现这一目标? - 请注意,在堆栈视图之前有一定数量的固定视图,因此以下添加堆栈视图并对其进行布局的代码可能看起来不必要地复杂,但这是必需的(除非有人可以建议更简洁的方法来做到这一点?)
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)
}
}
}
我不确定你在这里问的到底是什么。结果的一些演示可能会有所帮助...
但您似乎正在研究如何以编程方式设置约束以创建类似布局的列表。要对垂直(例如)这样做,应执行以下操作:
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))
}
}
}
我有一个 StackViews 数组,它是使用以下代码从 Web 服务创建的
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()
}
当我布局子视图时,我可以很好地添加视图,但是它们都相互重叠,因为我不知道如何添加约束来观察前一个视图的底部锚点。我如何实现这一目标? - 请注意,在堆栈视图之前有一定数量的固定视图,因此以下添加堆栈视图并对其进行布局的代码可能看起来不必要地复杂,但这是必需的(除非有人可以建议更简洁的方法来做到这一点?)
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)
}
}
}
我不确定你在这里问的到底是什么。结果的一些演示可能会有所帮助...
但您似乎正在研究如何以编程方式设置约束以创建类似布局的列表。要对垂直(例如)这样做,应执行以下操作:
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))
}
}
}