-1

I want to change the color of UIbuttons but problem is that I have tried changing color in

@IBAction func buttonPressed(_ sender: UIButton) {
    sender.backgroundColor = .red
}

it is not doing anything but on the same time some other buttons are changing colors on this method. I am really confused about this behaviour that some buttons are changing color on same method others don't.

I've also tried @IBOutlet method and it also didn't worked

@IBOutlet weak var button: UIButton!

and then change the color of button in method

button.backgroundColor = .red

Even setting text or color of the in viewDidLoad not working. It is showing the same button as it is designed in storyboard

Arslan Kaleem
  • 1,410
  • 11
  • 25
  • You must declare the button as `IBOulet` and set [this](https://stackoverflow.com/a/24427334/5941807) as action – Joannes May 26 '22 at 07:58
  • yeah I have declared the @IBOutlet as well and tried to change the backgroud color as well but didn't work – Arslan Kaleem May 26 '22 at 08:02
  • 2
    Check link connection in storyboard for `IBOutlet` and `Action` [the basics](https://www.youtube.com/watch?v=CVqdylyDSao) – Joannes May 26 '22 at 08:20
  • add the image of the coding which will be helpfull in solving the issue – Dark_Clouds_369 May 26 '22 at 08:38
  • 1
    My code file is too big. All of the buttons are working fine but this button isn't changing color. I have added `@IBAction` as well as `@IBOutlet` and tried both ways to change the background color of a button but its not working. There are also other buttons which are working fine. – Arslan Kaleem May 26 '22 at 08:48
  • Please check `IBOutlet` and `IBAction` connections for the button in storyboard, also check if `sender` instance is the same button that you're working on. – N4SK May 26 '22 at 09:54
  • @N4SK everything was connected and working fine just not changing the color and text of button which I figured out the solution and mentioned the solution in answer. Thanks everyone for your help :) – Arslan Kaleem May 26 '22 at 11:26

2 Answers2

2

So finally after hours of work I figure out the all connections are working and I have to set tintColor not a backgroundColor to change button color. I have tried changing tintColor before but I didn't see the change because backgroundColor was overriding it. All this confusion occured because. of two reasons.

1). I imagined that button color is backgroundColor but its was actually tintColor

2). There are almost three variables in storyboard with same backgroundColor as seen in given images which are overriding colors and even color was changing I was unable to see the change. As far as title change is concerned using this line did the trick

sender.setTitle("Button", for: .normal)

instead of this line

sender.titleLabel?.text = "Button"

First Image Second Image

Arslan Kaleem
  • 1,410
  • 11
  • 25
0

initialize the button like this

@IBOutlet weak var btnname: UIButton!

then in viewdidload or function

btnname.backgroundColor = .red
Dark_Clouds_369
  • 658
  • 1
  • 6
  • 24