import Foundation
class NSLayoutConstraintEx: NSLayoutConstraint {
@IBInspectable
var ipad: CGFloat = 0 {
didSet {
if DeviceUtils.isIPad() {
constant = ipad
}
}
}
@IBInspectable
var iphone4: CGFloat = 0 {
didSet {
// 640 x 960
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height == 960.0 {
constant = self.iphone4
}
}
}
@IBInspectable
var iphone5: CGFloat = 0 {
didSet {
// 640 x 1136
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height == 1136.0 {
constant = self.iphone5
}
}
}
@IBInspectable
var iphone6: CGFloat = 0 {
didSet {
// 750 x 1334
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height == 1334.0 {
constant = self.iphone6
}
}
}
@IBInspectable
var iphone6Plus: CGFloat = 0 {
didSet {
// 1242 x 2208
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height >= 1920.0 {
constant = self.iphone6Plus
}
}
}
}
class NSLayoutConstraintEx: NSLayoutConstraint {
@IBInspectable
var ipad: CGFloat = 0 {
didSet {
if DeviceUtils.isIPad() {
constant = ipad
}
}
}
@IBInspectable
var iphone4: CGFloat = 0 {
didSet {
// 640 x 960
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height == 960.0 {
constant = self.iphone4
}
}
}
@IBInspectable
var iphone5: CGFloat = 0 {
didSet {
// 640 x 1136
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height == 1136.0 {
constant = self.iphone5
}
}
}
@IBInspectable
var iphone6: CGFloat = 0 {
didSet {
// 750 x 1334
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height == 1334.0 {
constant = self.iphone6
}
}
}
@IBInspectable
var iphone6Plus: CGFloat = 0 {
didSet {
// 1242 x 2208
if DeviceUtils.isIphone() && DeviceUtils.getScreenSize().height >= 1920.0 {
constant = self.iphone6Plus
}
}
}
}
用法:
修改现在的约束,让它使用NSLayoutConstraintEx,效果如下:
注意User Defined Runtime Attributes这一栏,如果去掉某个自定义属性,这边可能会存在残留,删掉残留的那一项即可。还有Module这一栏如果显示None说明使用自定义类失败,一般把Class删除一下重新制定就好了。
默认是iPhone布局,这样在iPad上就能使用大尺寸的图片了
本文转自博客园农民伯伯的博客,原文链接:【iOS】屏幕适配之NSLayoutConstraint,如需转载请自行联系原博主。