Home > Blockchain >  swift Stack insertSubview & auto refresh layout problem
swift Stack insertSubview & auto refresh layout problem

Time:03-02

I would like to click and add subview at: index 3 position. But the constraint not update automatically. how can i refresh the layout when adding subview to uistack?

Thank you for your help very much

@objc func showMoreView() {
        stackView.insertSubview(view3, at: 3)
        stackView.superview?.layoutIfNeeded()
        view.layoutIfNeeded()
    }
    

Current Layout

 view.addSubview(stackView)
 stackView.addArrangedSubview(view1)       
 stackView.addArrangedSubview(view2)       
 stackView.addArrangedSubview(view4)
 stackView.addArrangedSubview(view5)

CodePudding user response:

You need to use

func insertArrangedSubview(_ view: UIView, at stackIndex: Int)

like that:

stackView.insertArrangedSubview(view3, at: 3)

Documentation here

CodePudding user response:

In stackview, Swift has make the function support insert a view to a position.

func insertArrangedSubview(_ view: UIView, atIndex stackIndex: Int)

I think it helpfully for you.

  • Related