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?