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

10,110

Solution 1

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.

Solution 2

Most simple way is what Rashwan L suggested , create an outlet just like logo1,smiley1 like this.

@IBOutlet weak var flButton: UIButton!

after that you can easily change your color by

flButton.alpha = 0
Share:
10,110
Codemaster99
Author by

Codemaster99

Updated on June 04, 2022

Comments

  • Codemaster99
    Codemaster99 almost 2 years

    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         
        }