如何更改SWIFT IOS中的导航栏背景?

时间:2015-08-17 15:23:11

标签: ios swift background uinavigationbar

我试图更改导航栏背景图片,但我收到了致命错误fatal error: unexpectedly found nil while unwrapping an Optional value

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

1 个答案:

答案 0 :(得分:1)

试试这个:

 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