ios 为什么我无法在导航栏中动态查看图片?

j9per5c4  于 2023-03-05  发布在  iOS
关注(0)|答案(1)|浏览(136)

为什么我无法在导航栏中动态查看图片?

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 20))
    imageView.contentMode = .scaleAspectFill
    
    imageView.sd_setImage(with: URL(string: currentFirma?.goruntuUrl ?? "")) { image, _, _, _ in
        self.navigationController?.navigationBar.topItem?.titleView = UIImageView(image: image)
    }
}
h4cxqtbf

h4cxqtbf1#

确保viewController嵌入在navigationController中,并编写如下代码

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 20))
imageView.contentMode = .scaleAspectFill
navigationItem.titleView = imageView
imageView.sd_setImage(with: URL(string: currentFirma?.goruntuUrl ?? ""), placeholderImage: UIImage(named: "")) // make sure to add a placeholder for better UX

相关问题