Igor Ševtšenko
Igor Ševtšenko

Reputation: 142

How to update UIButton in navbar rightItem each time when it is taped in UIKit

I created button for navbar with custom image and want to change its tint color each time user taps on it.

 let addToFavoritesButton = UIImage(systemName: SystemImages.FilledStar.rawValue)
 navigationItem.rightBarButtonItem = UIBarButtonItem(image: addToFavoritesButton, style: .plain, target: self, action: #selector(addOrRemoveFromFavorites))
 navigationItem.rightBarButtonItem?.tintColor = presenter.addToFavoriteButtonColor()

Upvotes: 0

Views: 54

Answers (1)

ygam
ygam

Reputation: 131

You should put this the change of the rightBarButtonItem tint color inside the function you created for the tap on the rightBarButtonItem

   @objc func addOrRemoveFromFavorites(_ sender: AnyObject) {
   navigationItem.rightBarButtonItem?.tintColor = 
presenter.addToFavoriteButtonColor()
}

this will only work if you have a UINavigationController.

Upvotes: 1

Related Questions