Transparent look at another transparent UIView with opaque content?

The idea is to show a dialogue of progress. I am trying to cover the parent view (usually the whole screen) with a transparent black representation (alpha 0.2). In addition, I want to show a smaller view, also transparent with a different color. It may not be affected by the color or transparency of the first kind. In this 2nd view, I want the user interface elements to be opaque. I tried everything and even found messages here that deal with something similar, but uses only one transparent layer. And they already use strange tricks. Is there a standard, easy way to achieve this?

+6
source share
5 answers

I found the answer on the blog: http://cocoawithlove.com/2009/04/showing-message-over-iphone-keyboard.html

The solution is to override drawRect: and take care of the alpha in it. You cannot touch the UIView's alpha property, nor can you set the background color of the view to anything other than transparent. All drawing should be done in drawRect:. In this way, I was able to stack transparent views and overlay opaque elements on top.

+3
source

Without any hack or complex and possibly slow custom drawRect: this is possible if you group your views:

Create a bounding view that spans and holds the entire dialogue. This view has no visible content, and its backgroundColor is understandable. Its alpha is 1.0.

Now add all the transparent views (those that have alpha <1) to what you want, and also add the opaque views. Be careful not to add any opaque representations as transparent routines, but instead add them as direct representations of a limited representation. Thus, they inherit their alpha 1.0.

 UIView *progressDialogView = [[UIView alloc] initWithFrame:dialogFrame]; progressDialogView.backgroundColor = [UIColor clearColor]; progressDialogView.alpha = 1.0; //you can leave this line out, since this is the default. UIView *halfTransparentBackgroundView = [[UIView alloc] initWithFrame:dialogFrame]; halfTransparentBackgroundView.backgroundColor = [UIColor blackColor]; //or whatever... halfTransparentBackgroundView.alpha = 0.5; [progressDialogView addSubview: halfTransparentBackgroundView]; UIView *progressBarView = [[UIView alloc] initWithFrame:progressBarFrame]; progressBarView.backgroundColor = [UIColor blueColor]; [progressDialogView addSubview: progressBarView]; 
+9
source

Instead of setting the alpha property, use to adjust the background color / opacity for each view:

 view1.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3]; 

Use whatever values ​​you want for red, green, blue and alpha.

+5
source

I needed to have a transparent view on top of another view. The view is transparent, what I just did:

 self.alpha = 0.5; 

For instance:

 - (void) configureView { self.frame = CGRectMake(0, 0, 640, 960); self.backgroundColor = [UIColor greyColor]; self.alpha = 0.5; } 
+2
source

Adding an opaque view as a subview of a transparent view will not work. What I have done in the past has a UIView class that adds a translucent subview and an opaque subview. Thus, a transparent view will not affect an opaque one.

+1
source

Source: https://habr.com/ru/post/906123/


All Articles