logankilpatrick
logankilpatrick

Reputation: 14521

How to get all visible UI elements in iOS at runtime?

How can I get all visible elements on screen at runtime in Swift 5.0 on the iOS Platform?

Upvotes: 1

Views: 930

Answers (1)

Volkan Sonmez
Volkan Sonmez

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

Related Questions