UINavigationBar setBackgroundImage in AppDelegate

2019-07-06 05:22发布

问题:

I am trying to set a background image to the navigation bar globally in the App Delegate in Swift. I can get it to work on an individual View Controller like this:

var topBar: UINavigationBar!

topBar.setBackgroundImage(UIImage(named: "navbar"), forBarMetrics:
.Default)

But when I try and add the following to the App Delegate, it crashes with an uncaught exception:

UINavigationBar.appearance().setBackgroundImage(UIImage(named:
"navbar"), forBarMetrics: .Default)

回答1:

OK, I deleted the navigation bars and added new ones in the storyboard... now it works with just the App Delegate code. It's just Xcode 6/Swift being buggy, I guess.