Applying Porperty to Every UIButton in current app

Hello,

Is there any way to iterate all views and subviews in current xcode app and taget to particular properties to every UIButton.

I tried using folllowing code but it is not working.

for views in self.view.subviews {
     if views == UIButton {
          button.isPointerInteractionEnabled = true
    } else {
            
    }
}

Replies

There's a couple of problems with your code:

  1. Assuming that self is a View Controller, you're only looking at its view property's subviews, and not its entire hierarchy. So for example, if view contained a Stack View of buttons, those buttons would be missed. You'd need to write a recursive function instead.

  2. if views == UIButton is not the correct way to check if a view is a button. You'd probably want to instead downcast it as such: if let button = subview as? UIButton

In your question you ask about to iterate all views in the running app. If the code above was correct, it would only iterate the buttons in whatever self is, which I presume is a single view controller and not the entire app.

However, there is a way to customize appearances in the entire app, using UIAppearance proxies. But this is only for properties that are marked with UI_APPEARANCE_SELECTOR. In the case of UIButton, that includes properties like titleColor and backgroundImage, not isPointerInteractionEnabled.