简体   繁体   中英

Swift - Store the stacking order of views

I used view.addSubview(imageView) to add two UIimageview on a UIview . How do I store the stacking order of them and reproduce them with the exact order? Any suggestions? I wish to store the stacking order information on firebase.

Here is how you can do it with the approach mentioned in the other answers (using subviews of the parent),

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

From Apple Doc,

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.

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

When you add a subview, it is stored in an array. To retrieve the array you have to write down just

view.subviews

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM