开发者

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

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 th开发者_如何学JAVAere any workaround?


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)


The parent that acts as your background should be changed to a sibling before the container that wraps your children. That way you can set the transparency without affecting the entire hierarchy.

This would look like this.

  • <item-container>
    • <item-background>
    • <children-container>
      • <children/>

This hierarchy, would have the same visibility order and let you set the opacity independently of the contents of the children container.


I know this is a late answer, and something has already been accepted, but I found the best solution for me is not to set the UIView's transparency directly, but instead set it's background color to 0,0,0,0, making the color fully transparent.


Not 100% sure what you are trying to do, but if you'd like to have a container view with a transparent background you can do so by setting the backgroundColor property to [UIColor clearColor]. That way you can see through the background, but all of the subviews will remain opaque. If you'd like the background to be semi-transparent just use [UIColor colorWithRed:green:blue:alpha:].

If you want the entire view to be semi-transparent while other views are not you'll have to make them siblings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜