Stranger B.
Stranger B.

Reputation: 9374

How to change the navigation bar background in SWIFT IOS?

I'm trying to change the navigations bar background image, but i'm getting a fatal error fatal error: unexpectedly found nil while unwrapping an Optional value

var image : UIImage = UIImage(named:"Quiz.png")!
        self.navigationController!.navigationBar.setBackgroundImage(image,
            forBarMetrics: .Default)

Upvotes: 0

Views: 137

Answers (1)

iAnurag
iAnurag

Reputation: 9346

try this:

 let logo = UIImage(named: "Quiz.png")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 38, height: 38))
imageView.contentMode = .ScaleAspectFit
imageView = UIImageView(image:logo)
self.navigationItem.titleView = imageView

Upvotes: 1

Related Questions