Swift - 存储视图的堆叠顺序

Swift - Store the stacking order of views

我用 view.addSubview(imageView)UIview 上添加了两个 UIimageview。如何存储它们的堆叠顺序并以准确的顺序重现它们?有什么建议么?我希望将堆叠顺序信息存储在 firebase 上。

来自苹果文档,

You can use this property to retrieve the subviews associated with your custom view hierarchies. The order of the subviews in the array reflects their visible order on the screen, with the view at index 0 being the back-most view.

For complex views declared in UIKit and other system frameworks, any subviews of the view are generally considered private and subject to change at any time. Therefore, you should not attempt to retrieve or modify subviews for these types of system-supplied views. If you do, your code may break during a future system update.

参考:https://developer.apple.com/documentation/uikit/uiview/1622614-subviews

添加子视图时,它存储在一个数组中。要检索数组,您只需写下

view.subviews

这里是如何使用其他答案中提到的方法(使用父视图的子视图),

if let indexOfImageView = view.subviews.firstIndex(where: {[=10=] === imageView})) {
    // Save index of image view
}