Reputation: 14521
How can I get all visible elements on screen at runtime in Swift 5.0 on the iOS Platform?
Upvotes: 1
Views: 930
Reputation: 785
You can do that basically.
Add this code on your ViewController. I recommend create a new swift file call it UIViewExtension.
extension UIView
{
public var allSubviews: [UIView]
{
return self.subviews.flatMap { [$0] + $0.allSubviews }
}
}
Now call this method when you need all visible elements.
public func allVisibleElements()
{
for subview in self.view.allSubviews
{
if (!subview.isHidden)
{
//ok you are done!.
}
}
}
Upvotes: 3