I have a storyboard. I loaded a custom UIView from nib file to one of my UIViewController programatically.
Opaque and alpha values are checked and 1 respectively.
Even though the UIView is semi-transparent.
What would be the problem here?
A subview's maximum alpha value is the alpha value of its parent view.
The alpha of the child view is effectively the product of the two alpha values. A child with alpha of 1 on a parent with alpha of 0.5 will effectively have a 0.5 alpha.
If you need to put an opaque subview on a translucent or transparent parent view, there are two approaches.
The first approach is to move the would-be parent view to a subview as well so that both views are subviews of the same parent view. That parent view is just a clear-background view that acts as a container but has an alpha of 1.
The second approach is to make the parent view transparent or translucent while leaving its alpha at 1. This is done by setting its colors to have non-1 alpha values. For example:
[UIColor colorWithRed:1 green:0 blue:0 alpha: 0.3];
Rather than just:
someView.alpha = 0.3;
Related
When I click on Share button. Open Share_view. and its alpha 0.5 but I am add another view on Share view.
But I want to this view alpha 1.0 and its not transparent I want to see full white.
See my Image
I had try this, but this is not working at all:
[Detail_view setBackgroundColor:[[UIColor White] colorWithAlphaComponent:1.0]];
As far as I understand, you are adding Detail_view on Share_view, hence Share_view is the parent for your Detail_view.
Solution -
Make Detail_view a subview of self.view, i.e
[self.view addSubview:Detail_view];
If you want Detail_view on Share_view, you can do this:
Don't set the alpha directly on the parent view. Instead use
[Share_view setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.5]];
Now any child view can have its own color and will NOT be transparent.
I have a view as the subview of another view, then I set the alpha value of the father view something like 0.5, but this also makes the subview transparent even when its alpha value is set to 1. So how can I make the subview non-transparent(opaque) when its father view has an alpha value less than 1?
The closest you're going to get is colorWithAlphaComponent:. Using something like the following, you can set the alpha component of the parent view's background, and it won't affect subviews.
[yourSuperview setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.5]];
If you set the parent view to 0.5 all the subviews will also go to 0.5 or less. You'll have to come up with a different design approach.
For Swift 4.2
self.yoursuperview.backgroundColor = UIColor.black.withAlphaComponent(0.5)
The following method worked for me.
superView.backgroundColor = .clear
Now that alpha property of superview is not altered, it won't affect the alpha of its subview. Hence, subviews will be opaque(if you have a background color for it).
You should have two designs for the parent view. One would be the "enabled" design, showing it as it is and the other would be the "disabled" design, which would contain transparent background.
You cannot do this with alpha, as subviews inherit the alpha. If
parent.alpha = x
and
view.alpha = y,
then the real alpha of the view will be:
x * y
I think, You have create View by Storyboard. I had already face this problem. Find the below code, this is correct in my case.
[MyParentView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.6]];
Hope, This may help you.
I have UIView with lots of subviews (UILabel, UITextView, etc.).
If a set alpha 0.6 to main view all the subviews takes this alpha.
How to set alpha separately of main view?
[view setBackgroundColor:[[UIColor clearColor] colorWithAlphaComponent:0.5]];
//try this.. dont try to set alpha of UIView and also your subviews will not affect
myView.layer.shouldRasterize = YES
This will make it use group opacity and everything should composite as you'd expect.
I have a view that is slightly translucent, I set the alpha to about .75 and it contains a button as a sub element.
I want the button to be completely opaque and I set the opaque property in IB but still the button appears as translucent.
Any pointers?
Thanks!
A subview of a superview is allways minimum as transparent as its subview.
Subview alpha = 0.5 and superview alpha = 0.1 would result in an alpha of the subview of 0.05.
The only way of achieving that is changing the view hierarchy. Your subview must not be a subview any more. It may still be at the same position. (May require different position value).
An example.
Your background view is backgroundView
Your superview is firstView
Your subview is overlayView
Your current hierarchy is
backgroundView -> firstView -> overlayView
You should change that to:
backgroundView -> first View
\-> overlay View.
If your firstView's position is (10, 10) and your overlayView's position was (20,20) then change your overlayView's position to (30,30) because it now is within the coordinate system of background view.
For your code:
At some point you used to have
[backgroundView addSubView:firstView];
[fisrtView addSubView:overlayView];
change that to
[backgroundView addSubView:firstView];
[backgroundView addSubView:overlayView];
If you did not code that but defined it within IB then just move overlayView within the views tree accordingly so that it is on the same level as firstView.
Regardless whether you code it or draw it in IB, make sure that firstView comes first and overlayView comes next. Otherwise firstView could hide overlayView when its alpa is greater than 0.
I'm developing an iOS 4 application.
I have a main view that contains another UIView, named shadowView, with an UIWebView inside. This shadowView has black as background color and alpha = 0.3f.
My problem is that UIWebView inherits shadowView alpha value, and I don't want that, I need UIWebView has alpha = 1.0.
Do you know how can I do that?
If I understand your question, you have added a UIWebView to a UIView which has an alpha of 0.3.
On iOS, any sub-views inherit their parent views alpha values (or rather, the parent view 'masks' the sub-view).
It sounds as if you want your shadowView to have a translucent background: rather than setting the alpha of the view, you should instead do this:
[shadowView setAlpha:1.0];
[shadowView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.3]];`
...which will (as long as your view isn't set to be opaque) give you a nice translucent background and allow your sub-views to not appear transparent as well.