Xcode检测iOS版本并相应地显示故事板
我想向iOS6用户显示一个故事板,另一个显示给iOS7用户。 我怎样才能做到这一点?
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { //load and show ios6 storyboard } else { //load and show ios7 storyboard }
您可以使用此代码获取iOS版本:
[[UIDevice currentDevice] systemVersion]
例如,要检测iOS 6,您可以执行如下操作:
if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) { // ... }
然后,为iOS 6和7加载不同的故事板,您可以执行如下操作:
if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) { myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_6" bundle:nil]; } else { myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_7" bundle:nil]; }
编辑:正如在其他答案中指出的,一个可以更好地检测iOS版本的方法是使用NSFoundationVersionNumber,因为没有stringparsing的systemVersion是必要的。
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) { myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_6" bundle:nil]; } else { myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_7" bundle:nil]; }
你可以在AppDelegate中尝试这样的事情(非常重要)
UIStoryboard *storyboard = nil; if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) { storyboard = [UIStoryboard storyboardWithName:@"iOS7_AND_ABOVE" bundle:[NSBundle mainBundle]]; } else { storyboard = [UIStoryboard storyboardWithName:@"iOS_below_7" bundle:[NSBundle mainBundle]]; }