Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView: opaque vs. alpha vs. opacity

How do opaque alpha and the opacity of the background work together for a UIView and what are the differences between them?

UIView http://i.minus.com/jb2IP8TXbYTxKr.png

like image 310
Besi Avatar asked Dec 15 '11 12:12

Besi


People also ask

Is Alpha same as opacity?

Alpha affects everything drawn on the view. The background color's alpha affects the background color's transparency and anything else drawn on the view. i.e it brings about a frosty effect to whole view. Opacity means don't draw anything underneath, even if you are transparent, it just effects the current view.

What is opaque view in Swift?

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 opacity in IOS?

iOSApps/ApplicationsMobile Development. 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.

What is opacity in Xcode?

Updated for Xcode 14.0 beta 1. 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.


2 Answers

opaque means don't draw anything underneath, even if you are transparent.

The background color's alpha only affects the background color's transparency, not anything else drawn on the view.

alpha affects everything drawn on the view.


The opaque property can give you a speed increase - if you know that your view will never have transparency you can set this to YES and when iOS renders your view it can make some performance optimisations and render it faster. If this is set to NO iOS will have to blend your view with the view underneath, even if it doesn't happen to contain any transparency.

The alpha will also affect the alpha of the backround color i.e. if the background color is 0.5 transparent and the alpha is also 0.5, this has the effect of making the background view's alpha 0.25 (0.5 * 0.5).

like image 155
deanWombourne Avatar answered Oct 29 '22 03:10

deanWombourne


To the very good answer by deanWombourne it's worth to add that, unless you don't draw your own content using the drawRect: method, the opaque property has no effect.

Apple's doc:

You only need to set a value for the opaque property in subclasses of UIView that draw their own content using the drawRect: method. The opaque property has no effect in system-provided classes such as UIButton, UILabel, UITableViewCell, and so on.

If you draw your own content, keep in mind, that opaque is just a hint

This property provides a hint to the drawing system as to how it should treat the view.

and some more guidelines from the same Apple's doc:

If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content, the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.

like image 24
Mikolaj Avatar answered Oct 29 '22 03:10

Mikolaj