首页 > 解决方案 > Xcode 9 布局:有没有办法为 iPad 构建一个与 iPhone 相同内容的新故事板?

问题描述

我在为所有设备获取带有约束的布局时遇到问题,所以我的问题是,如果这是一种为 iPad 构建具有相同代码、segues 等的额外故事板的方法,那么正确获取布局会更容易。

标签: iosiphoneswiftxcodeipad

解决方案


是的,有办法,您可以复制现有故事板并重命名它iPad_Main.storyboard,然后您必须更正一些约束或更改适合 iPad 屏幕的值。

之后在初始化任何视图控制器之前,您必须检查设备是 iPad 还是 iPhone,并且基于此您必须使用情节提要。

let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom
let storyboard : UIStoryBoard!
if (deviceIdiom == .pad) {
   storyboard = UIStoryboard(name: "iPad_Main", bundle: nil)
}
else {
   storyboard = UIStoryboard(name: "Main", bundle: nil)
}

// Use storyboard to get desired controller using StoryboardID of UIViewController

推荐阅读