title: How to show one different image in a button toggle when tapped?
body: <p>I have a setting of a Button inside a function in a UIViewController extension. I set the two images for the two different state. But this doesn't change when the button is pressed. How I can fix this?</p>
  
  <p>I tried all the solutions provided here.</p>
  
  <pre><code>import Foundation
  import UIKit
  
  extension UIViewController {
  
      func addSortAndWeatherButton(sortAction: Selector, weatherAction: Selector){
  
      let sortButton: UIButton = UIButton(type: UIButton.ButtonType.custom)
  
      sortButton.setImage(UIImage(named: "sort-near.png"), for: .normal)
      sortButton.setImage(UIImage(named: "sort-rating.png"), for: .selected)
  
      sortButton.addTarget(self, action: sortAction, for: .touchUpInside)
      sortButton.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
      let sortBarButton = UIBarButtonItem(customView: sortButton)
  
      self.navigationItem.rightBarButtonItems = sortBarButton
      }
  </code></pre>