swift - Programmatic layout : UIStackView alignment doesn't seem to work -
i have created uiviewcontroller
uistackview
(vertical axis) wrapped in uiscrollview
that's pinned edges of root view
auto-layout constraints.
i have generated number of uibutton
s , added arranged subviews
of uistackview
.
i have tried no avail centre align uibuttons
in uistackview
.
i'm not i'm doing wrong.
here's code:
import uikit class viewcontroller: uiviewcontroller { var scrollview: uiscrollview! var stackview: uistackview! override func viewdidload() { super.viewdidload() view.backgroundcolor = uicolor.whitecolor() scrollview = uiscrollview() scrollview.translatesautoresizingmaskintoconstraints = false view.addsubview(scrollview) view.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[scrollview]|", options: .alignallcenterx, metrics: nil, views: ["scrollview": scrollview])) view.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|[scrollview]|", options: .alignallcenterx, metrics: nil, views: ["scrollview": scrollview])) stackview = uistackview() stackview.translatesautoresizingmaskintoconstraints = false stackview.axis = .vertical stackview.alignment = .center scrollview.addsubview(stackview) scrollview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:|[stackview]|", options: nslayoutformatoptions.alignallcenterx, metrics: nil, views: ["stackview": stackview])) scrollview.addconstraints(nslayoutconstraint.constraintswithvisualformat("v:|[stackview]|", options: nslayoutformatoptions.alignallcenterx, metrics: nil, views: ["stackview": stackview])) _ in 1 ..< 100 { let vw = uibutton(type: uibuttontype.system) vw.settitle("button", forstate: .normal) stackview.addarrangedsubview(vw) } } }
an equal width constraint between scrollview
, stackview
needed. this:
scrollview.addconstraints(nslayoutconstraint.constraintswithvisualformat("h:[stackview(==scrollview)]", options: .alignallcenterx, metrics: nil, views: ["stackview": stackview, "scrollview": scrollview]))
that did me.
Comments
Post a Comment