Search code examples
swiftlabelview

How to set Alpha of a view with out affecting its sub-views and its own text?


I have a view with label button an image-view. I have set the view alpha to be 0.5. So when ever I add a sub view this sub view appear to be affected by the alpha of the view. I don't mean its value but for the person eye it seems it has the same alpha. So I want to find a way to add sub-view that appears 100% clear with the view as background and its alpha is 0.5.

Also I want a text of a label not affected by the label Alpha. is that possible ?


Solution

  • Setting a view's alpha property affects all of its subviews as well.

    If you just want the main view to have a partially transparent background color, set the color with an alpha value less than 1.0.

    If you need other parts of the view to have an alpha, then your only choice is to add this view to a container view. And instead of adding subviews to your original view, add the subviews to this new container view. Then you can set the alpha on the original view as needed.