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

Time:01-31

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()

CodePudding user response:

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.

  •  Tags:  
  • Related