Swifty Swifty - 2 years ago 186
iOS Question

How do I adjust the transparency (alpha) of a button in Swift?

My button is

flButton
but when I try to set the
alpha
to
0
, it gives me the error


Value of type '(Any) -> ()
has no member
alpha
".


Do I have to replace
Any
in the
@IBAction
line?

import UIKit

class FirstLaunchView: UIViewController {

@IBOutlet weak var logo1: UIImageView!
@IBOutlet weak var smiley1: UIImageView!
@IBOutlet weak var subHapp1: UILabel!
@IBOutlet weak var happ1: UILabel!
@IBAction func flButton(_ sender: Any) {

UserDefaults.standard.set(false, forKey: "name")
//launchDetector = false

performSegue(withIdentifier: "toMain", sender: self)
}

@IBOutlet weak var firstLaunch: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

logo1.alpha = 0
smiley1.alpha = 0
subHapp1.alpha = 0
happ1.alpha = 0
flButton.alpha = 0
}

Answer Source

Your flButton is an @IBAction function. You should either create an @IBOutlet for your button or do this in your function if you want to keep it:

if let button = sender as? UIButton {
    button.alpha = 0
}

Or you could just change the type of you @IBAction function from:

@IBAction func flButton(_ sender: Any)

To:

@IBAction func flButton(_ sender: UIButton)

And then just set sender.alpha = 0.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download