UIStackView addArrangedSubview function reorders arrangedSubviews when providing view that is already in arrangedSubviews array

Originator:miikro
Number:rdar://FB8015604 Date Originated:15.07.2020
Status:Open Resolved:
Product:UIKit Product Version:
Classification: Reproducible:
 
Let’s say I have UIStackView instance where arrangedSubviews are [X, Y]. When I call addArrangedSubview(X) arrangedSubviews will be following array: [Y, X] which means that order of subviews has been altered.

According to Apple docs (https://developer.apple.com/documentation/uikit/uistackview/1616227-addarrangedsubview)
"This method automatically adds the provided view as a subview of the stack view, if it is not already. If the view is already a subview, this operation does not alter the subview ordering."

code:

import UIKit

let stackView = UIStackView()
stackView.axis = .horizontal

let textField = UITextField()
let button = UIButton()

stackView.addArrangedSubview(textField) // [UITextField]
stackView.addArrangedSubview(button) // [UITextField, UIButton]
stackView.addArrangedSubview(textField) // [UIButton, UITextField]

Comments

It turns out that's an expected behaviour and "operation does not alter the subview ordering" part applies to z position.

"Thank you for filing this feedback report. We reviewed your report and determined the behavior you experienced is currently functioning as intended.

-[UIStackView addArrangedSubview:] behaves intentionally exactly as -[UIView addSubview:] and -[CALayer addLayer:], and if the view (or layer) is already in the relevant array, it’s moved to the end.

The documentation is a little confusing. “This operation does not alter the subview ordering” means that the subview z-ordering doesn’t change; i.e., the order of the array in the -subviews property does not change. However, the -arrangedSubviews ordering does change, as we stated before."


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!