Custom UINavigationBar works on simulator but not

2019-07-03 23:35发布

问题:

I'm hoping to get some insight into an issue I've been struggling to wrap my head around for the past few hours. I have a custom UINavigationBar that is being configured in application:DidFinishLaunchingWithOptions by calling the following method:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar"] forBarMetrics:UIBarMetricsDefault];

When I run my application in XCode (both in the Simulator and on an actual device that is plugged in to the computer), this works great and my custom background is displayed. However, when I run a release version of the app on TestFlight, the standard light blue navigation bar is showing. After messing with placement of the setBackgroundImage method with no luck, I have come to believe it has to do with differences in "debug" vs. "release" settings, but I am clueless as to where those settings would cause this navigationBar issue.

Any help or insight into this issue (or how I would go about debugging it) would be greatly appreciated. Thank you in advance!

回答1:

As mentioned in my comments, when you are facing similar issue you should check the name of images and make sure it is not case sensitive. You might have added an image with name navBar but you are using as NavBar in your project. That is the reason for this issue. Simulator is not case sensitive but device is.

Either change the code to,

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navBar"] forBarMetrics:UIBarMetricsDefault];


回答2:

In the device image names are treated as case sensitive. Also you need to supply it with extension.

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar.**png**"] forBarMetrics:UIBarMetricsDefault];

So check exact name and extension.