13

How can one programmatically reorder items within a UIStackView in IOS?

(i.e. I can not see a function at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/#//apple_ref/doc/uid/TP40015256-CH1-SW30 to cover this)???

Greg
  • 34,042
  • 79
  • 253
  • 454

2 Answers2

41

When adding a view to a parent view, it is automatically removed from it's old parent view.

This is the same with arrangedView, so you only need to use insertArrangedSubview(_:atIndex:) and addArrangedSubview().

Using removeArrangedSubview() is not needed.

Aviel Gross
  • 9,770
  • 3
  • 52
  • 62
16

surely you can use a combination of

removeArrangedSubview(_:)
insertArrangedSubview(_:atIndex:)

to reorder your views?

Fonix
  • 11,447
  • 3
  • 45
  • 74
  • do you know offhand if that would give a "flicker" from an UI perspective? e.g. the uiView might appear somewhere else on the screen for a fraction of a second and then jump back into the stackview? I should try it... – Greg Jun 09 '16 at 05:50
  • it shouldnt, since all the operations of removing and adding will happen before the layout is refreshed, so all changes will appear at once on the next drawing cycle (if you dont do something strange) – Fonix Jun 09 '16 at 05:51
  • 23
    @Greg I've waited every day for the past 484 days to hear back from you! I can't keep silent any more – Greg Hilston Oct 06 '17 at 16:25
  • @GregHilston hahaha – Fonix Oct 09 '17 at 08:16