简体   繁体   中英

How i can use image in navigation bar title in swift ios

i'm in trouble. i want to use image in navigation bar title, but i'm getting a error ie "UIImage" is not a subtype of "NSString". Below the code, look it.

class Dashboard: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = UIImage(named: "logo.png")        
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Anyone can suggest? Thanks!

Set the navigationItem's titleView

var image = UIImage(named: "logo.png")
self.navigationItem.titleView = UIImageView(image: image)

worked for me

        let image : UIImage = UIImage(named: "headerLogo")
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    imageView.contentMode = .scaleAspectFit
    imageView.image = image
    navigationItem.titleView = imageView

Swift 5+,iOS 13+

navigationItem.titleView = UIImageView(image: UIImage(named: "id_logo"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM