我正在使用“防御性”代码将自定义图像设置为 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];
}