iOS how to make subview of a transparent view opaque? - ios

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.

Related

How to set this Detail View Alpha 1.0 , non transparent in ios?

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.

nib file custom view added programmatically is semi-transparent

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;

How to correctly set alpha of UIView? [iOS]

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.

UIView border cover sub-view?

I have a UIView which includes a UIButton which is partially on UIView. I have a problem when I draw a border on my UIView. Please have a look at my screenshot:
You can see the border is above the UIButton, why? Can anybody suggest? Thanks
Thanks for aăâ, I found a solution.
Basically the border is always drawn on top of everything
What I did is:
Create a UIView with color of border
Create another UIView as the child the main UIView which is a little bit smaller than the first one. The color of this newly create UIView is the main color
Here is the code:
self.layer.cornerRadius = 15;
self.layer.masksToBounds = YES;
self.backView.layer.cornerRadius = 15;
self.backView.layer.masksToBounds = YES;
The result is:
It's more or less what I need although it's not perfect.
It could have to do with the order that the objects are drawn. In your storyboard's "Document Outline", views that are lower down in a view controller's outline are drawn later. Perhaps the button is not the last drawn view, like you want?

UIWebView over an UIView gets UIView alpha value

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.

Resources