如何检查特定iOS版本上是否有可用的功能?

She*_*lam 6 iphone cocoa-touch appearance objective-c uinavigationbar

在我的AppDelegate中,我使用外观代理来制作自定义UI:

//Setup custom appearances
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"header"] forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBar appearance] setTintColor:[UIColor blackColor]];
    [[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"header"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Run Code Online (Sandbox Code Playgroud)

这将在iOS4中崩溃.如何检查这个功能是否在他们运行的iOS版本上可用,这样我可以避免崩溃?

Jos*_*erg 19

不检查操作系统,检查功能是否存在.

if ([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {}

  • Apple不建议检查版本.正如@joshua所说,你应该寻找你感兴趣的功能. (5认同)
  • 令人震惊的.我没有看到检查操作系统版本有任何损害.仅仅存在符号并不会使其底层实现在OS版本之间相等. (2认同)