Animate MainView without SubView iOS

I have a laid out views in Storyboard. My structure has the following elements:

  • Off Button Background (UIView), Main View
    • Off Icon (UIButton), Sub View
    • Off Button Label (UILabel), Sub View

Here's how that looks:

Storyboard in Xcode

Now, I want to animate the "Off Button Bg" to alpha = 0, but leave the "Off Icon" and "Off Button Label" at alpha=1.

Is that possible given my current structure, where the subviews are nested under "Off Button Bg"? Ideally, I'd like to keep the subviews nested under "Off Button Bg" to enable animating all these elements as a whole in other places.


That is not possible given your current structure. You won't be able to nest those views as subviews without affecting the alpha of those views when you change the alpha of the UIView. The documentation of the alpha property states

However, the transparency imparted by that alpha value affects all of the view's contents, including its subviews. For example, a subview with an alpha value of 1.0 that is embedded in a parent view with an alpha value of 0.5, appears onscreen as if its alpha value is also 0.5.

However, you can achieve that by placing the Off Icon (button) and Off Button Label at the same level in the hierarchy as Off Button Bg, and then setting the constraints of Off Button Bg to align with the other views, but you'll have to manage the alpha values of the views independently.

Animate MainView without SubView iOS
See more ...