Xcode 检测 iOS 版本并相应地显示故事板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19186189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:00:57  来源:igfitidea点击:

Xcode detect iOS version and show storyboard accordingly

iosxcodestoryboardios7

提问by BrownEye

I want to show one storyboard to my iOS6 users and another to iOS7 users. How can I do this?

我想向我的 iOS6 用户展示一个故事板,向 iOS7 用户展示另一个。我怎样才能做到这一点?

回答by Mike Pollard

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
    //load and show ios6 storyboard
}
else {
    //load and show ios7 storyboard
}

回答by Greg

You can get the iOS version using this code:

您可以使用以下代码获取 iOS 版本:

[[UIDevice currentDevice] systemVersion]

For example, to detect iOS 6, you could do something like:

例如,要检测 iOS 6,您可以执行以下操作:

if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) {
    // ...
}

Then, to load different storyboards for iOS 6 and 7, you would do something like:

然后,要为 iOS 6 和 7 加载不同的故事板,您可以执行以下操作:

if ([[UIDevice currentDevice].systemVersion hasPrefix:@"6"]) {
    myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_6" bundle:nil];
} else {
    myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_7" bundle:nil];
}


EDIT:as noted in the other answers, an arguably better way to detect iOS version is to use the NSFoundationVersionNumber, as no string parsing of the systemVersion is needed.

编辑:如其他答案所述,检测 iOS 版本的更好方法是使用 NSFoundationVersionNumber,因为不需要对 systemVersion 进行字符串解析。

if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
    myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_6" bundle:nil];
} else {
    myStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_7" bundle:nil];
}

回答by danypata

You can try something like this in AppDelegate (very important)

你可以在 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]];
 }