ios - swift 每种屏幕尺寸的不同 StoryBoard

标签 ios objective-c iphone swift uikit

如何在 Swift 中为每种可能的屏幕尺寸设置不同的 Storyboard?

我已经有了 Objective-C 代码。

请不要自动布局,我不需要它。

但是如何将它转换为 Swift 呢?我是 Swift 的新手。

这是 Objective-C 的代码:

AppDelgate.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    // int screenHeight = [UIScreen mainScreen].bounds.size.height;
    // NSLog(@"Screen Height is %i", screenHeight);

    // grab correct storyboard depending on screen height
    UIStoryboard *storyboard = [self grabStoryboard];

    // display storyboard
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];

    return YES;
}

- (UIStoryboard *)grabStoryboard {
    // determine screen size
    int screenHeight = [UIScreen mainScreen].bounds.size.height;
    UIStoryboard *storyboard;

    switch (screenHeight) {
            // iPhone 4s
        case 480:
            storyboard = [UIStoryboard storyboardWithName:@"Main-4s" bundle:nil];
            break;

            // iPhone 5s
        case 568:
            storyboard = [UIStoryboard storyboardWithName:@"Main-5s" bundle:nil];
            break;

            // iPhone 6
        case 667:
            storyboard = [UIStoryboard storyboardWithName:@"Main-6" bundle:nil];
            break;

            // iPhone 6 Plus
        case 736:
            storyboard = [UIStoryboard storyboardWithName:@"Main-6-Plus" bundle:nil];
            break;

        default:
            // it's an iPad
            storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            break;
    }
    return storyboard;
}

最佳答案

如果您只想将代码转换为 Swift,您可以引用 Apple 的 swift 教程。您可以在 iBook Store 或 Web 上查看这本书。为了您的引用,我将代码转换为 Swift。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let splitViewController = self.window!.rootViewController as! UISplitViewController
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
    navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
    splitViewController.delegate = self
    return true


    // int screenHeight = [UIScreen mainScreen].bounds.size.height;
    // NSLog(@"Screen Height is %i", screenHeight);

    // grab correct storyboard depending on screen height

    let storyboard = grabStoryboard()

    // display storyboard
    self.window?.rootViewController = storyboard.instantiateInitialViewController()
    self.window?.makeKeyAndVisible()

    return true
}

func grabStoryboard() -> UIStoryboard
{
    // determine screen size
    let screenHeight = UIScreen.mainScreen().bounds.size.height
    var storyboard: UIStoryboard! = nil

    switch (screenHeight)
    {
    // iPhone 4s
    case 480:
        storyboard = UIStoryboard(name: "Main-4s", bundle: nil)
    // iPhone 5s
    case 568:
        storyboard = UIStoryboard(name: "Main-5s", bundle: nil)
    // iPhone 6
    case 667:
        storyboard = UIStoryboard(name: "Main-6", bundle: nil)
    // iPhone 6 Plus
    case 736:
        storyboard = UIStoryboard(name: "Main-6-Plus", bundle: nil)
    default:
    // it's an iPad
        storyboard = UIStoryboard(name: "Main", bundle: nil)
    }

    return storyboard
}

关于ios - swift 每种屏幕尺寸的不同 StoryBoard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34486873/

相关文章:

ios - 输入类型 ='file' 在 Instagram 应用内浏览器中不起作用

objective-c - objective-c : convert a NSMutableString in NSString

iphone - iOS 做什么而不是轮询近乎实时的数据?

ios - 外部框架的 gitignore 路径

ios - 如何以编程方式获取 ios 中的内存使用信息?

ios - 我需要在 iOS7+ 应用程序中实现 didFailToReceiveAdWithError 吗?

objective-c - libcURL 代码不工作,说我正在上传到没有文件名的服务器

iphone - iOS 5:UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage:不起作用/被忽略

iphone - 触摸时淡出 UIButton

ios - 架构 armv7 :"_OBJC_CLASS_$_KTKRegion"的 undefined symbol ,引用自 : objc-class-ref in AppDelegate. o