「防御」コードを使用して、カスタム イメージを UINavigationBar バーに設定しています。これは、ios5 以降のデバイスでうまく機能します。
いくつかの回答の後、回答を組み合わせてコードを編集しています(以下を参照)
これは正しい & エレガントな方法ですか?
if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
UIImage *backgroundImage = [UIImage imageNamed:@"tableTitleView.png"];
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
}
else
{
NSString *barBgPath = [[NSBundle mainBundle] pathForResource:@"tableTitleView" ofType:@"png"];
[self.navigationController.navigationBar.layer setContents:(id)[UIImage imageWithContentsOfFile: barBgPath].CGImage];
}