简体   繁体   English

每种屏幕尺寸都可以使用不同的StoryBoard

[英]Different StoryBoard for each screen size swift

How do I set Different Storyboards for each possible screen size in Swift? 如何在Swift中为每种可能的屏幕尺寸设置不同的Storyboard?

I already have the Objective-C code. 我已经有了Objective-C代码。

Please, no Auto Layout, I don't need it. 拜托,没有自动版面配置,我不需要。

But how do I convert it to Swift? 但是如何将其转换为Swift? I am new to Swift. 我是Swift的新手。

Here is code for Objective-C: 这是Objective-C的代码:

AppDelgate.m file 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;
}

If you just want to convert the code to the Swift you could reference the swift tutorial from Apple. 如果您只想将代码转换为Swift,则可以参考Apple的swift教程。 You can check the book in the iBook Store or on the web. 您可以在iBook商店或网络上查看这本书。 For your reference I converted the code into the Swift. 供您参考,我将代码转换为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
}

Load different storyboard as per screen size: 根据屏幕大小加载不同的故事板:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM