iPhone Programming: Applying Alpha to Parent but not to Child Views

Mustafa picture Mustafa · Oct 17, 2009 · Viewed 12.3k times · Source

Is it possible to set Alpha to .75 or .50 (transparent) for Parent view but the Child views/controls are opaque (Alpha channel 1.0 - completely visible).

Whenever i set Alpha of my main UIView to transparent, all the child views/controls are also shown as transparent. Is there any workaround?

Answer

Abhinav picture Abhinav · Jul 10, 2010

One simpler way is also possible without making the views siblings. Don't set the alpha directly on the parent view. Instead use

[parentView setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];

Now any child view can have its own color and will NOT be transparent.

In Swift, this would be

parentView.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.7)