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?

2
  • Did you look at this stackoverflow.com/questions/39066126/…? Commented May 1, 2020 at 5: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. Commented May 1, 2020 at 12:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.