Stackview exchange or change order of views

Ankish Jain picture Ankish Jain · Dec 26, 2016 · Viewed 10.3k times · Source

Stackview containing array of textfield is embedded in scrollview. I want to change the order of the text fields on some actions. The way to remove and add the textfield results in distorted view. I also tested by removing from scrollview. Normal stackview too the exchange does not appear properly. I am using indexes to change :


stackView.removeArrangedSubview(localityTF) stackView.insertArrangedSubview(localityTF, at: 2)

Answer

Andrew Romanov picture Andrew Romanov · Dec 26, 2016

It is strange behaviour, but problem with the autolayout system, that should be updated before you can add localityTF back. Also don't forget that removeArrangedSubview does not remove the view from subviews array:

[self.stackView removeArrangedSubview:_label1];
[self.stackView setNeedsLayout];
[self.stackView layoutIfNeeded];

[self.stackView insertArrangedSubview:_label1 atIndex:2];   
[self.stackView setNeedsLayout];