Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

colorWithAlphaComponent example in Swift

What is the correct syntax for this function in Swift?

The following works fine, and colors the background purple:

 self.view.backgroundColor = UIColor.purpleColor() 

When I chain the colorWithAlphaComponent function, the view shows the correct alpha for a moment, and then changes to an opaque purple that is relatively dark:

 self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.5) 

Is this the recommended function for adding an alpha value to a UIColor?

Furthermore, why does the intellisense popup say that this function expects a UIColor as a parameter? E.g.,

   self.view.backgroundColor = UIColor.colorWithAlphaComponent(<#UIColor#>) 

EDIT: The behavior is strange. I am setting the background color on a view controller that is being loaded in a modal. As the modal slides up from the bottom, the alpha is correct. When the modal finishes loading, the background color changes to opaque?!

EDIT 2: The problem was not with the code--both the code above and the suggestion below were properly applying the alpha. The issue is the way that modals are being presented--the underlying view is being removed. See:
Transparent Modal View on Navigation Controller

like image 773
kmiklas Avatar asked Jun 20 '14 19:06

kmiklas


People also ask

What is Alpha in UIColor?

alpha. The opacity value of the new color object, specified as a value from 0.0 to 1.0.

What is Alpha in Swift 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 CGColor in Swift?

CGColor is the fundamental data type used internally by Core Graphics to represent colors. CGColor objects, and the functions that operate on them, provide a fast and convenient way of managing and setting colors directly, especially colors that are reused (such as black for text).


1 Answers

It's not strange, it's behaving exactly as it should. Although many of UIColor's methods are class methods, there are still a few instance methods, and this is one of them. From the UIColor documentation.

colorWithAlphaComponent:

Creates and returns a color object that has the same color space and component values as the receiver, but has the specified alpha component.

So, colorWithAlphaComponent: just changes the alpha value of its receiver. Example:

let purple = UIColor.purpleColor() // 1.0 alpha let semi = purple.colorWithAlphaComponent(0.5) // 0.5 alpha 

And the reason why you're seeing autocompletion for this instance method on the type, is because Swift allows you to use instance methods as curried type methods. In the example you provided, colorWithAlphaComponent actually returns a function that takes a CGFloat as input and returns a UIColor.

let purple = UIColor.purpleColor() let purpleFunc: (CGFloat -> UIColor) = UIColor.colorWithAlphaComponent(purple) 

So, if you wanted to, you could call the type method passing in the instance you want to modify, and then call the resulting function with the alpha that you want to apply, like so.

let purple = UIColor.purpleColor() let purpleTrans = UIColor.colorWithAlphaComponent(purple)(0.5) 

Then as far as the issues you're having with the modal view controller go, you shouldn't be attempting to change the alpha of the view of a modal view controller. See this for more info. Instead, you should be manually creating a view and adding it to the view hierarchy of your existing view controller (if you absolutely have to alter its alpha)

like image 177
Mick MacCallum Avatar answered Oct 08 '22 06:10

Mick MacCallum