Skip to content

Commit

Permalink
Merge pull request #10 from cruisediary/fix/fix-get-unknownsize
Browse files Browse the repository at this point in the history
Fix/fix get unknownsize
  • Loading branch information
cruisediary authored Dec 17, 2016
2 parents 34100d2 + b98a2e8 commit 51e59e4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 9 additions & 2 deletions Example/Tests/DeviceLayoutConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import XCTest

import Device
import Quick
import Nimble

Expand Down Expand Up @@ -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
}

Expand All @@ -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") {
Expand All @@ -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
}
}
8 changes: 5 additions & 3 deletions Sources/DeviceLayoutConstraint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)}}
Expand All @@ -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()
Expand Down

0 comments on commit 51e59e4

Please sign in to comment.