51

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

Community
  • 1
  • 1
kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • out of curiosity have you tried `UIColor(red: 0.5, green: 0, blue: 0.5, alpha: 0.5)`? – fqdn Jun 20 '14 at 20:03
  • I just tried; same behavior as when using `.colorWithAlphaComponent(0.5)`. See edit. – kmiklas Jun 20 '14 at 20:07
  • strange, `colorWithAlphaComponent` is a instance method and not class method. If this is defined as class method then as per the functionality it should take a color and float as parameters. – nprd Jun 20 '14 at 20:52
  • I agree... strange. You see it also, then? – kmiklas Jun 20 '14 at 20:52
  • http://stackoverflow.com/questions/24336187/how-to-present-a-modal-atop-the-current-view – kmiklas Jun 20 '14 at 21:55

6 Answers6

94

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)

Community
  • 1
  • 1
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Thx. I'm trying to "grey out" the background when the modal appears. In other words, the actual modal content only takes up about 300x200pt on the screen. I want the area outside the content "grayed out." When the user taps thereon, it will close the modal. – kmiklas Jun 20 '14 at 21:13
  • In one of the other answers (lower down), a setting is identified: `The easiest way is to use modalPresentationStyle property of navigationController` – kmiklas Jun 20 '14 at 21:16
  • 1
    Swift 3 update: no longer "colorWithAlphaComponent(_:)". It's now just "withAlphaComponent"... so purple.withAlphaComponent(0.5) per the example. – davidrynn Feb 26 '17 at 00:36
  • *"So, colorWithAlphaComponent: just changes the alpha value of its receiver"* I don't think so. It creates a copy of its receiver with its alpha value changed; the alpha of the receiver itself isn't changed, I believe. – Pang Apr 26 '17 at 09:58
  • Swift 3: color.withAlphaComponent(0.5) –  Sep 19 '17 at 05:22
46

Swift 5.0

self.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
Arjun Yadav
  • 1,369
  • 1
  • 10
  • 23
7

in Swift 3.0

This works for me in xcode 8.2.

yourView.backgroundColor = UIColor.black.withAlphaComponent(0.5)

It may helps you.

Ashu
  • 3,373
  • 38
  • 34
6

Since UIColor is part of UIKit, it has been replaced in SwiftUI with Color. The equivalent method is .opacity(_ opacity: Double) for example:

Color.purple.opacity(0.5)
JoGoFo
  • 1,928
  • 14
  • 31
3

Try smth like this to set color

 view.backgroundColor = UIColor(red: (64/255.0), green: (54/255.0), blue: (105/255.0), alpha: 1.0)
Mikhail Valuyskiy
  • 1,238
  • 2
  • 16
  • 31
-2

UIColor.black.withAlphaComponent(0.5).cgColor