如何根据 iPhone 大小在 Splash 中设置不同的约束?

How to set different constraints in a Splash depending on the iPhone size?

我正在做一个项目,其中一项要求是,公司徽标必须出现在距离 iPhone 屏幕尺寸顶部 10% 的位置(如果它是 iPhone 5, 568高度,应该是57)。我可以应用约束,但据我所知,它是一个固定值而不是相对值。 我一直在检查 Xcode 的 "Vary for traits" 功能,但由于我需要的所有尺寸都是 wC hR,因此在这种情况下不起作用。 另一种解决方案是根据 iPhone 模型应用不同的飞溅,但我不确定是否可行。

有什么想法吗?谢谢!

您可以使用占位符视图来创建您需要的垂直 space。

在徽标上方添加一个UIView

将此视图的顶部限制在根视图的顶部 将此视图的底部限制在徽标的顶部 将此视图的高度限制为根视图高度的 1/10

您也可以在代码中轻松实现。

logoImageView.translatesAutoresizingMaskIntoConstraints = false

// This will keep your imageView 10% from the top
NSLayoutConstraint(item: logoImageView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .topMargin, multiplier: 1.0, constant: self.view.frame.size.height/10).isActive = true

// Additional You will want your imageView to be centered.
NSLayoutConstraint(item: logoImageView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0).isActive = true