如何根据屏幕大小加载特定的故事板
How to load a specific storyboard depending on size of screen
我正在尝试开发一个与 3.5 英寸和 4 英寸显示器兼容的应用程序。但是,当我尝试使用自动布局时,我不知道如何根据屏幕大小调整视图大小。我能想到的解决这个问题的唯一其他方法是设计两个故事板并根据 phone 屏幕的大小加载一个。有人可以帮我解决这个问题吗?谢谢你的时间。
查看情节提要中的 size classes。您可以指定相同组件的其他布局,在不同设备上使用相同的代码。
如果你真的想使用不同的故事板,在方法application:didFinishLaunchingWithOptions:
的AppDelegate中设置它。这里可以通过
获取屏幕尺寸
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
并使用
设置故事板
let storyboard = UIStoryboard(name: "Main", bundle: nil)
window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("InitVC") as UIViewController
window!.makeKeyAndVisible()
此代码将使用故事板 Main
和初始视图控制器 InitVC
实例化应用
AutoLayout 无法针对 iPhone 的特定模型。但是,有一个解决方法。你有两个选择。
第一个选项
放置一个不可见的 UIView
并创建顶部、底部、前导和尾随约束 Superview
。之后将您的对象(它们可以是 UIImageViews
、UILabels
等等)放在您刚刚放置的 UIView
中。现在创建将对象连接到 UIView
.
的顶部和底部约束
我创建了一个演示来向您展示它是如何完成的。正如您将看到的,按钮具有不同的大小,具体取决于屏幕的大小。从 this link.
下载演示
第二个选项
您可以通过编程方式创建和修改约束。创建一个 NSLayoutConstraint
并根据用户的设备调整它的 constant
:
var myConstant: CGFloat = 0
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
myConstant = 10
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// iPhone 5
myConstant = 20
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
myConstant = 30
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
myConstant = 40
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
myConstant = 50
}
var myConstraint = NSLayoutConstraint (item: myButton,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Top,
multiplier: 1,
constant: myConstant)
self.view.addConstraint(myConstraint)
我正在尝试开发一个与 3.5 英寸和 4 英寸显示器兼容的应用程序。但是,当我尝试使用自动布局时,我不知道如何根据屏幕大小调整视图大小。我能想到的解决这个问题的唯一其他方法是设计两个故事板并根据 phone 屏幕的大小加载一个。有人可以帮我解决这个问题吗?谢谢你的时间。
查看情节提要中的 size classes。您可以指定相同组件的其他布局,在不同设备上使用相同的代码。
如果你真的想使用不同的故事板,在方法application:didFinishLaunchingWithOptions:
的AppDelegate中设置它。这里可以通过
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
并使用
设置故事板let storyboard = UIStoryboard(name: "Main", bundle: nil)
window!.rootViewController = storyboard.instantiateViewControllerWithIdentifier("InitVC") as UIViewController
window!.makeKeyAndVisible()
此代码将使用故事板 Main
和初始视图控制器 InitVC
AutoLayout 无法针对 iPhone 的特定模型。但是,有一个解决方法。你有两个选择。
第一个选项
放置一个不可见的 UIView
并创建顶部、底部、前导和尾随约束 Superview
。之后将您的对象(它们可以是 UIImageViews
、UILabels
等等)放在您刚刚放置的 UIView
中。现在创建将对象连接到 UIView
.
我创建了一个演示来向您展示它是如何完成的。正如您将看到的,按钮具有不同的大小,具体取决于屏幕的大小。从 this link.
下载演示第二个选项
您可以通过编程方式创建和修改约束。创建一个 NSLayoutConstraint
并根据用户的设备调整它的 constant
:
var myConstant: CGFloat = 0
if UIScreen.mainScreen().bounds.size.height == 480 {
// iPhone 4
myConstant = 10
} else if UIScreen.mainScreen().bounds.size.height == 568 {
// iPhone 5
myConstant = 20
} else if UIScreen.mainScreen().bounds.size.width == 375 {
// iPhone 6
myConstant = 30
} else if UIScreen.mainScreen().bounds.size.width == 414 {
// iPhone 6+
myConstant = 40
} else if UIScreen.mainScreen().bounds.size.width == 768 {
// iPad
myConstant = 50
}
var myConstraint = NSLayoutConstraint (item: myButton,
attribute: NSLayoutAttribute.Top,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Top,
multiplier: 1,
constant: myConstant)
self.view.addConstraint(myConstraint)