首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同的StoryBoard为每个屏幕大小迅速

不同的StoryBoard为每个屏幕大小迅速
EN

Stack Overflow用户
提问于 2015-12-28 02:03:57
回答 2查看 2.6K关注 0票数 0

如何在Swift中为每个可能的屏幕大小设置不同的故事板?

我已经有目标C代码了。

拜托,不要自动布局,我不需要它。

但是我如何把它转换成Swift呢?我是斯威夫特的新手。

以下是目标C的代码:

AppDelgate.m文件

代码语言:javascript
复制
- (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;
}
EN

回答 2

Stack Overflow用户

发布于 2015-12-28 02:29:57

如果你只想把代码转换成Swift,你可以参考苹果的快速教程。您可以在iBook商店或网上查阅这本书。供您参考,我将代码转换为Swift。

代码语言:javascript
复制
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
}
票数 3
EN

Stack Overflow用户

发布于 2015-12-28 06:52:04

按屏幕大小加载不同的情节提要:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34486873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档