Setting alpha on UIView sets the alpha on its subviews which should not happen
Asked Answered
N

10

93

According to the documentation for UIVIew @property(nonatomic) CGFloat alpha

The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. This value affects only the current view and does not affect any of its embedded subviews.

I have a container view configured as follows:

self.myView.backgroundColor = [UIColor blackColor];
self.myView.alpha = 0.5;
[self addSubview:self.myView];

And then add subviews to 'myView'

[myView addSubView anotherView];
anotherView.alpha = 1;
NSLog(@"anotherView alpha = %f",anotherView.alpha); // prints 1.0000 as expected

But 'anotherView' does have alpha on screen (it is not opaque as expected)

How can this be and what can be done?

Norvil answered 8/9, 2013 at 8:33 Comment(3)
Maybe the order of adding subviews and setting alpha is important. Try playing with different sequences.Bereniceberenson
Add all code of creation of anotherView :) and also i thinks it is typo but are you sure you initialize self.myView ? and add anotherView such like [self.self addSubview:self.myView];Antirachitic
indeed, the documentation is correct: that won't affect the its embedded subviews and the subviews' alpha are always the same – but the rendered views have an alpha value which is all subviews' alpha values, multiplied. e.g. if the subviews alpha 0.8 and the superview's alpha was 1.0, but you change it to 0.6, the subviews alpha is still the same, 0.8. the rendered subview's alpha value is changed only from 0.8 to 0.48.Wilk
E
124

I think this is a bug in the documentation. You should file it at bugreport.apple.com.

Everything I can see after a bit of quick research suggests what you are seeing is how it always has behaved, and my own testing shows it too.

The alpha of a view is applied to all subviews.

Perhaps all you need is [[UIColor blackColor] colorWithAlphaComponent:0.5] but if not you will need to make the view a sibling instead of a child.

Entebbe answered 8/9, 2013 at 8:52 Comment(3)
+1. FWIW, I think the docs are arguably correct, if extremely unclear (to the point of being incorrect for all practical purposes). The alpha of the subviews is affected, but not the alpha value. This is the same behaviour, I think, for the hidden property. Setting hidden hides subviews, but doesn't cause subviews hidden property to be set.Reconstructionist
i had requirement to show a view some thing like uipopover but in iphone.I have to create a uiviewcontroller with [[UIColor blackColor] colorWithAlphaComponent:0.5].thanks it workedMonody
I don't believe the docs are "arguably correct" at all. In the real world, devs will read the docs and believe that child alphas are unaffected. When, in reality, the composited result will be as if the alphas had been modified. It's just a case of bad documentation.Overnight
S
44

Don't set the alpha directly on the parent view. Instead of it use the below line of code which will apply transparency to parentview without affecting its child views.

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

Sandlin answered 29/5, 2014 at 5:12 Comment(3)
this code is not working with latest ios and xcode . anyone has different ideas ?Struthious
Excellent!! For swift 4 use: parentView.backgroundColor=UIColor.black.withAlphaComponent(0.5)Filtration
Great solution.Havana
M
32

In swift

view.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.5)

UPDATED FOR SWIFT 3

view.backgroundColor = UIColor.white.withAlphaComponent(0.5)
Mesarch answered 15/3, 2016 at 19:1 Comment(4)
This doesn't address the question asked.Brooder
This gives me an error of: "Value of type 'UIColor' has no member 'colorWithAlphaComponent'".Bust
Hello @Krivvenz, 'colorWithAlphaComponent' has been renamed to 'withAlphaComponent'. See my edited answer.Mesarch
this code is not working . we can not see pushed view as transparent . did anyone has any other idea ?Struthious
Z
27

Set Opacity of the background color instead of alpha will not affect its child views.

  1. select view.
  2. go to attribute inspector than background color
  3. click on "others"
  4. set opacity to 30%

Or you can set by programmetically

var customView:UIView = UIView()
customView.layer.opacity = 0.3

Thats it. Happy Coding!!!

Zeculon answered 28/12, 2017 at 21:15 Comment(10)
Is there any way to do this programmatically?Militarist
@DCIndieDev Sorry i haven't found opacity as a property of UIView. So set by storyboard.Zeculon
This is a great answer. Worked great.Bromeosin
This should be the accepted answer, Worked as needed. Thanks.Multimillionaire
@DCIndieDev did you find any property of opacity?Zeculon
@Zeculon You can access the opacity property from the [your UIView].layer.opacityMultimillionaire
@Multimillionaire Ohh. Sorry i didn't notice it in view layer. Thank you Missa.Zeculon
Great answer. I had a hard time figuring it out. Thanks.Dentiform
This still affects the subviews.Tiffinytiffy
can you explain more what exactly you are trying.? @TiffinytiffyZeculon
O
17

If you like Storyboards, put a User Defined Runtime Attribute for your view in the Identity Inspector:

Key Path: backgroundColor, Type: Color, Value: e.g. white color with Opacity 50 %.

Odum answered 5/8, 2014 at 12:54 Comment(1)
Great answer! In the Attributes Inspector I set the view's alpha to one. In the Identity Inspector I insert key path as above with Opacity 50%. The view's opacity is now at 50%, but the opacity of the subviews is still 100%. I suppose that alpha and opacity are NOT one in the same.Chevet
I
6

Simplest solution as discussed is to change the alpha as follows : Updated version for Xcode 8 Swift 3 is :

yourParentView.backgroundColor = UIColor.black.withAlphaComponent(0.4)

Objective C:

yourParentView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];

Refer Apple Developer Docs here : https://developer.apple.com/reference/uikit/uiview/1622417-alpha

Incurious answered 14/12, 2016 at 11:39 Comment(7)
yes i applied this , but view did not get transparent . i have used this code earlier many times . but this time its not working . and showing my view as solid black color . so any alternate method or it has changed with new iOS updates ?Struthious
Most probably the view behind your yourParentView must be solid black please check your storyboard for xib and also your code that whether it is set to black from any where else. Hope this helpsIncurious
i am just pushing another view from one view . the pushed view would appear transparent after this code . i have used it many times earlier . it is as simple as this . --> settings *set = [[settings alloc] initWithNibName:@"settings" bundle:nil]; --> set.view.backgroundColor = [ [UIColor blueColor] colorWithAlphaComponent:0.3f]; --> [self.navigationController pushViewController:set animated:YES ];Struthious
are you using xib or storyboard?Incurious
but now i am using this code and it is not working . pushed view will appear with solid color whichever color we have given , and not transparent .Struthious
i am using xib . and used this kinda methodology earlier .Struthious
this code is not working . we can not see pushed view as transparent . did anyone has any other idea ?Struthious
S
6

In Swift 4.2 and Xcode 10.1

Don't add colour and alpha value through storyboard. Only programmatic approach will work in this case.

transparentView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
Sullen answered 3/4, 2019 at 9:27 Comment(1)
@iOS Thank u , this one helped me.Cubage
K
2

Here is a bit complex solution:

UIView *container;
UIView *myView;
UIView *anotherView;

myView.alpha = 0.5;
[container addSubview:myView];

anotherView.alpha = 1;
[container addSubview:anotherView];

Use a container view as superview, anotherView and myView are both subview in container, anotherView is not a subview in myView.

Klink answered 17/2, 2016 at 3:39 Comment(0)
P
2

For now there is only one way make the Parent View transparent and don't put any child views inside (don't put any views as subview) the parent view, put that child views outside of the parent view. To make parent view transparent you can do this via storyboard.

//Transparent the parentView

parentView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)

Put the other view outside of the parent view. It will work like a charm.

Prayerful answered 26/4, 2018 at 13:30 Comment(0)
G
1

Please refer to the bold description from Xcode documentation.

The value of this property is a floating-point number in the range 0.0 to 1.0, where 0.0 represents totally transparent and 1.0 represents totally opaque. Changing the value of this property updates the alpha value of the current view only. 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.

Grubby answered 2/7, 2019 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.