5

i'm in trouble. i want to use image in navigation bar title, but i'm getting a error i.e "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!

1
  • You can't assign an object of type UIImage to a NSString. The navigationItem's title can only hold textual data (NSString). Commented Nov 1, 2014 at 15:48

3 Answers 3

9

Set the navigationItem's titleView

var image = UIImage(named: "logo.png")
self.navigationItem.titleView = UIImageView(image: image)
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Swift 5+, iOS 13+

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

Comments

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.