簡體   English   中英

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

[英]Different StoryBoard for each screen size swift

如何在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商店或網絡上查看這本書。 供您參考,我將代碼轉換為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
}

根據屏幕大小加載不同的故事板:

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