Swift中的多个故事板

时间:2015-07-02 04:27:44

标签: ios iphone swift

我有一个有4个故事板的应用程序,每个用于不同的设备(iphone4,5,6,6 +)。我曾尝试使用自动约束,但由于我的应用程序很复杂,我无法弄明白。我听说如果你有多个故事板,应该有一种方法来指定在app委托中使用哪个故事板。如何为app委托中的正确设备指定正确的故事板?我目前有四个故事板,名为:

iphone4s.storyboard
iphone5.storyboard
iphone6.storyboard
iphone6plus.storyboard

谢谢。

3 个答案:

答案 0 :(得分:3)

据我所知,我们现在使用 autoLayout ,它知道如何根据屏幕尺寸和约束来布局视图

正如您所问,您可以在applicationdidFinishLaunchingWithOptions方法中从AppDelegate覆盖故事板

    let storyBoard : UIStoryboard = UIStoryboard(name: "iphone4s", bundle: nil)
    //you can check your device and override the storyboard name
    let initialViewController: UIViewController =  storyBoard.instantiateInitialViewController()!
    self.window?.rootViewController? = initialViewController

答案 1 :(得分:1)

实现一个在指定名称时检索故事板的函数。

func getStoryBoard(name : String)-> UIStoryboard
{
    var storyBoard = UIStoryboard(name: name, bundle: nil);
    return storyBoard;
}

更改应用委托方法,如:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
    var storyBoard = getStoryBoard("Main") // Change the storyboard name based on the device, you may need to write a condition here for that
    var initialViewController: UIViewController = storyBoard.instantiateInitialViewController() as! UIViewController
    self.window?.rootViewController = initialViewController
    self.window?.makeKeyAndVisible()
    // Override point for customization after application launch.
    return true
}

答案 2 :(得分:1)

为了为各种设备启动不同的故事板,您需要覆盖rootViewController对象。

       var myDeviceStoryboard : UIStoryboard = UIStoryboard(name: "DeviceStoryboard", bundle: NSBundle.mainBundle())
var firstVC : MyDeviceFirstViewController = myDeviceStoryboard.instantiateViewControllerWithIdentifier("MyDeviceFirstViewController") as! MyDeviceFirstViewController
self.window?.rootViewController = MyDeviceFirstViewController //This should be a navigation controller

此外,您需要在项目设置和故事板中覆盖一些设置。请参阅附件。enter image description here