0

This one is driving me crazy. I'm trying to change the RightBarButton from a function within the owning view controller (For more info, this is my structure: TabBarController > Navigation Controller > View Controller, with a unique Navigation Controller for each View Controller within the TabBarController)

func setProfileImage(url: String, action: Selector) {
    print("Set profile image")

    // Reset Profile Image
    var barButton = UIBarButtonItem()
    let button = UIButton(type: .custom)

    let profileImage = UIImageView()
    let storage = Storage.storage()
    var reference: StorageReference!
    reference = storage.reference(forURL: url)
    reference.downloadURL { (url, error) in

    let data = NSData(contentsOf: url!)
    let image = UIImage(data: data! as Data)
    profileImage.image = image ?? UIImage(named: "sullyPlaceholder")

    button.addTarget(self, action: action, for: .touchUpInside)
    button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
    button.clipsToBounds = true
    button.layer.cornerRadius = 0.5 * button.bounds.size.width
    button.layer.borderWidth = 3
    button.layer.borderColor = UIColor.lightGray.cgColor
    button.setImage(profileImage.image, for: .normal)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalToConstant: 35).isActive = true
    button.heightAnchor.constraint(equalToConstant: 35).isActive = true
    barButton = UIBarButtonItem(customView: button)
    self.navigationItem.setRightBarButton(barButton, animated: true)
    print("Profile Image Generated")

    }
}

Setting the image the first time on viewDidAppear works perfectly fine, but when I try to run the function from an action within the same view controller, it will not change. When I switch tabs and come back, the button & image is updated. How do I update/refresh the navigationItem from within the VC?

BaxiaMashia
  • 115
  • 10
  • Did you look at this https://stackoverflow.com/questions/39066126/how-to-properly-refresh-a-uinavigationbar? – Harsh May 01 '20 at 05:25
  • @Harsh Thanks for the help. I did look at this one, but unfortunately I'm working with iOS13 and as it states, it's not reliable for iOS13. I've "solved" this problem by manually calling ViewDidLoad() and it works with no apparent side-affects aside from memory usage, but I just find it strange that it's this difficult to make such a simple change. There must be a reason. – BaxiaMashia May 01 '20 at 12:12

0 Answers0