rice888
rice888

Reputation: 51

Change bar button item programmatically after user presses it Swift

I am trying to make a feature where if the user presses the speaker button (a bar button item), then a speaker with a slash through it will replace the original button. Is there an easy way of doing this? I know that it must be done programatically but I can't figure out how to replace the speaker button with a different button.

[Here is a screenshot of my storyboard] : https://i.sstatic.net/EL3df.png

Upvotes: 0

Views: 80

Answers (1)

Frankenstein
Frankenstein

Reputation: 16341

Add an action for UIBarButtonItem on your UIViewController class by control dragging it into the ViewController's declaration. Then change the item

@IBAction func barButtonAction(_ sender: UIBarButtonItem) {
    sender.image = UIImage(named: "myNewImage")
}

Upvotes: 1

Related Questions