Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have an opaque UIView as a subview of a semi-transparent UIView?

Tags:

ios

iphone

ipad

I have a UIView with an alpha of 0.5 which I add as a subview to my primary view in order to gray-out everything else. I want to add an additional UIView to this gray UIView as a subview - the problem is that when I do this, my newly-added subview is also partially transparent.

Is there any way to make a subview "ignore" the alpha value of its superview and be itself fully opaque?

like image 265
MusiGenesis Avatar asked May 24 '11 14:05

MusiGenesis


People also ask

What is opaque iOS?

An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1. 0 . If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable.

What is SwiftUI opacity?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.

What does Alpha mean in Swift?

Basic Swift Code for iOS Apps View's Alpha value 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.


2 Answers

Set the UIView background color alpha not it's alpha directly.

Objective-C

UIView *view; ... view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6]; 

It's not the same as:

view.backgroundColor = [UIColor blackColor];     view.alpha = .6; 

Swift

view.backgroundColor = UIColor.black.withAlphaComponent(0.6) 
like image 104
Darkngs Avatar answered Oct 03 '22 11:10

Darkngs


No, not really. What you want is to take your overlay view, and make it just have a clear background color. As a subview of that new overlay place your view that will grey things out. And as a sibling view to that put your view you want to be opaque.

[OpaqueView] [DimmingView]      |             |       [OverlayView] 
like image 28
Joshua Weinberg Avatar answered Oct 03 '22 11:10

Joshua Weinberg