我需要使用" for in"为stackview添加视图。循环。但是,stackview中的视图彼此重叠。
addplantoPlanArray充分初始化一个视图并将其添加到PlanArray,然后使用" for"循环将该数组中的每个视图添加到堆栈视图中。
这是代码:
self.addPlantoPlanArray(IDText: "123123", Name: "abc", Participants: "abc", Duration: "abc", Price: "abc")
self.addPlantoPlanArray(IDText: "14123123", Name: "123123", Participants: "123123", Duration: "123123", Price: "123123")
for plan in PlanArray {
stackview.addArrangedSubview(plan)
}
stackview.axis = .horizontal
stackview.distribution = .fillEqually
stackview.spacing = 274
view.addSubview(stackview)
结果是第二个视图在第一个视图上完全重叠。
stackview的约束:
view.addSubview(PlanStackView)
PlanStackView.translatesAutoresizingMaskIntoConstraints = false
let stackviewTop = NSLayoutConstraint(item: PlanStackView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 380)
let stackviewLeading = NSLayoutConstraint(item: PlanStackView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 10)
view.addConstraints([stackviewTop, stackviewLeading])
实例视图没有约束,它是带默认帧的init。这是实例类的init方法:
init(PlanDefaultIDText: String, PlanNameText: String, PlanParticipantsText: String, PlanDurationText: String, PlanPriceText: String) {
super.init(frame: CGRect(x: 0, y: 0, width: 264, height: 150))