4

I need to adding views to stackview by using "for in" loops. However, the views in the stackview are overlap with each other.

addplantoPlanArray samply init a view and add it to the PlanArray, then using "for in" loop to add each view in that Array into the stackview.

here is the code:

    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)

The result is that the second view is totally overlap on the first one.

The constrains for the 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])

No constrains for the instance view, it is init with default frame. here is the init method for the instance class:

    init(PlanDefaultIDText: String, PlanNameText: String, PlanParticipantsText: String, PlanDurationText: String, PlanPriceText: String) {
    super.init(frame: CGRect(x: 0, y: 0, width: 264, height: 150))
Gang Wei
  • 63
  • 1
  • 6
  • Need help... This sucks me for two hours. adding views into the stackview individually is fine, but when using For in Loops, it sucks. – Gang Wei Dec 09 '17 at 05:32
  • I'm not sure why you would need `274` points as spacing. That's code smell to me. What device are you using? – mfaani Dec 09 '17 at 06:04
  • Can you also show us the constriants of the stackview itself. did you set the stackView's `translatesautoresizingmaskintoconstraints` to `false`? If that didn't work then we also need to see the constraints of the `plan` instance as well. – mfaani Dec 09 '17 at 06:05
  • I update the constrains for the stackview, thanks. the 274 help to make it looks good, but does not help when adding a third view. – Gang Wei Dec 09 '17 at 06:30
  • **1. Where is your stackView's trailing and bottom constraints?!** 2. It's not vital rather *cleaner* if your `init` method would just take 1 model that encapsulates all its Strings, so you wouldn't have to pass to it like this ie do `Struct Plan{ var defaultIDText: String; nameText: String, ParticipantsText: String; duration: String; prince: String}` and then instantiate your view with `init(plan: Plan){ self.plan = plan; super.init(frame: CGRect(x: 0, y: 0, width: 264, height: 150)) }` – mfaani Dec 09 '17 at 23:12
  • Many Thanks I see what you mean. – Gang Wei Dec 10 '17 at 00:17
  • Problem solved by changing the plan class to plan struct. – Gang Wei Dec 11 '17 at 03:36
  • not sure how that solves a layout problem... – mfaani Dec 11 '17 at 03:46

0 Answers0