How to correctly set alpha of UIView? [iOS] - 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.

Related

Make part of UIView visible

I want to add a "shadow" image on my view, but, i want part of my view to still be "visible". You better understand what i want to do, when look on screenshot:
I can add a UIView above my superview, but how could i make specific point "visible"? That actually mean make specific area of a view with different colour or opaque.
For your problem I had a way, try the below example.
1. First storyboard design: I had a UIButton
2. Added black View as a subview
UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
blackView.backgroundColor=[UIColor blackColor];
blackView.alpha=0.6f;
[self.view addSubview:blackView];
now result will be:
3. Add one more UIImageView with frame equal to button frame
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=self.button.frame; //getting current UIButton bounds
imageView.image=[UIImage imageNamed:#"add_img.png"];
[self.view addSubview:imageView];
Now it looks same as what you want:
You have to add some views. A main view will be the container of your less-alpha view and of the opaque button.
MainView
|
------- UIView with 0.6 alpha
|
------- UIButton with 1 alpha
Indeed, if you change the alpha of the MainView, all subviews will be affected. Here, UIView with 0.6 will have the same frame as MainView but it will not affect UIButton alpha.
Make a new View, a parent View with clear color background. Add the black view and the button to the parent view and set the black view's alpha to 0.6 or whatever.
Try this:
Make a UIView called buttonBackgroundView, give it a black color and then
[self.view addsubView:buttonBackgroundView];
now make a UIView named plusView (or UIButton if this plus sign is a button) then
[self.view addsubView:plusView];
After that, give the alpha to the buttonBackgroundView
buttonBackgroundView.alpha = 0.6;
Happy coding!

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.

iOS how to make subview of a transparent view opaque?

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.

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;

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