【问题标题】:Different StoryBoard for each screen size swift每个屏幕尺寸的不同 StoryBoard swift
【发布时间】:2016-04-01 21:51:00
【问题描述】:

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

我已经有了 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;
}

【问题讨论】:

标签: ios objective-c iphone swift uikit


【解决方案1】:

如果您只想将代码转换为 Swift,您可以参考 Apple 的 swift 教程。您可以在 iBook Store 或网络上查看这本书。供您参考,我将代码转换为 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
}

【讨论】:

  • 我的目标是为每个不同的屏幕尺寸使用不同的 StoryBoard 而不是自动布局,我已经这样做是客观的 c 但不知道如何做到这一点很快。所以你能帮我吗谢谢
【解决方案2】:

根据屏幕尺寸加载不同的故事板:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    // 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
   var storyboard: UIStoryboard = self.grabStoryboard()
    // display storyboard
    self.window.rootViewController = storyboard.instantiateInitialViewController()
    self.window.makeKeyAndVisible()
    return true
}


func grabStoryboard() -> UIStoryboard {
    // determine screen size
    var screenHeight: Int = UIScreen.mainScreen().bounds.size.height
    var storyboard: UIStoryboard
    switch screenHeight {
    // iPhone 4s
    case 480:
        storyboard = UIStoryboard.storyboardWithName("Main-4s", bundle: nil)
    // iPhone 5s
    case 568:
        storyboard = UIStoryboard.storyboardWithName("Main-5s", bundle: nil)
    // iPhone 6
    case 667:
        storyboard = UIStoryboard.storyboardWithName("Main-6", bundle: nil)
    // iPhone 6 Plus
    case 736:
        storyboard = UIStoryboard.storyboardWithName("Main-6-Plus", bundle: nil)
    default:
    // it's an iPad
        storyboard = UIStoryboard.storyboardWithName("Main", bundle: nil)
    }

    return storyboard
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多