You need to download the image first. So for the download time you either setup a placeholder image or don't show it at all. But this code should help you.
fund viewDidLoad() {
super.viewDidLoad()
let placeholderImage = UIImage(named: "placeholder")
updateLeftNavigationItem(with: placeholderImage)
loadNavigationBarImage(from: URL(string: "<your url>")!)
}
func updateLeftNavigationItem(with image: UIImage?) {
navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(handleButtonPressed))
}
func loadNavigationBarImage(from url: URL) {
getDataFromUrl(url: url) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async() { [weak self] in
let image = UIImage(data: data)
self?.updateLeftNavigationItem(with: image)
}
}
}
func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url) { data, response, error in
completion(data, response, error)
}.resume()
}
For the initial state