diff --git a/Example/Tests/DeviceLayoutConstraintTests.swift b/Example/Tests/DeviceLayoutConstraintTests.swift index b234eb0..80e1eb4 100644 --- a/Example/Tests/DeviceLayoutConstraintTests.swift +++ b/Example/Tests/DeviceLayoutConstraintTests.swift @@ -8,6 +8,7 @@ import XCTest +import Device import Quick import Nimble @@ -39,7 +40,7 @@ class DeviceLayoutConstraintTests: QuickSpec { describe("DeviceLayoutConstraint") { context("when use iPhone6") { beforeEach { - sut.deviceSize = .screen5_5Inch + sut.expectedDeviceSize = .screen5_5Inch sut.inch5_5 = Expected.Constant.inch5_5 } @@ -60,7 +61,7 @@ class DeviceLayoutConstraintTests: QuickSpec { context("when deviceSize is 4inch device") { beforeEach { - sut.deviceSize = .screen4Inch + sut.expectedDeviceSize = .screen4Inch } context("when set constant per deviceSize") { @@ -84,8 +85,14 @@ class DeviceLayoutConstraintTests: QuickSpec { } class DeviceLayoutConstraintSpy: DeviceLayoutConstraint { + var expectedDeviceSize: Size! var layoutIfNeededCalledCount: Int = 0 + override open func layoutIfNeeded() { layoutIfNeededCalledCount += 1 } + + override open func deviceSize() -> Size { + return expectedDeviceSize + } } diff --git a/Sources/DeviceLayoutConstraint.swift b/Sources/DeviceLayoutConstraint.swift index b84f0ea..02f2ec8 100644 --- a/Sources/DeviceLayoutConstraint.swift +++ b/Sources/DeviceLayoutConstraint.swift @@ -12,8 +12,6 @@ import Device class DeviceLayoutConstraint: NSLayoutConstraint { - open var deviceSize: Size = Device.size() - @IBInspectable var inch3_5: CGFloat = 0.0 { didSet { updateConstant(size: .screen3_5Inch, constant: inch3_5)}} @IBInspectable var inch4: CGFloat = 0.0 { didSet { updateConstant(size: .screen4Inch, constant: inch4)}} @IBInspectable var inch4_7: CGFloat = 0.0 { didSet { updateConstant(size: .screen4_7Inch, constant: inch4_7)}} @@ -23,12 +21,16 @@ class DeviceLayoutConstraint: NSLayoutConstraint { @IBInspectable var inch12_9: CGFloat = 0.0 { didSet { updateConstant(size: .screen12_9Inch, constant: inch12_9)}} fileprivate func updateConstant(size: Size, constant: CGFloat) { - if size == self.deviceSize { + if size == deviceSize() { self.constant = constant layoutIfNeeded() } } + open func deviceSize() -> Size { + return Device.size() + } + open func layoutIfNeeded() { self.firstItem.layoutIfNeeded() self.secondItem?.layoutIfNeeded()