-
Notifications
You must be signed in to change notification settings - Fork 0
/
SZDeviceSpecificLayoutConstraint.m
148 lines (118 loc) · 3.5 KB
/
SZDeviceSpecificLayoutConstraint.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// SZDeviceSpecificLayoutConstraint.m
// DevSpecLayoutConstraintDemo
//
// Created by Сергей Галездинов on 31.07.15.
//
//
#import "SZDeviceSpecificLayoutConstraint.h"
typedef NS_ENUM(NSUInteger, SZDeviceType) {
szdtUnknown,
szdtIPhone4s,
szdtIPhone5,
szdtIPhone6,
szdtIPhone6P,
MAX_DEVICE
};
@implementation SZDeviceSpecificLayoutConstraint {
BOOL _valuesSet[MAX_DEVICE];
}
SZDeviceType currentDeviceType = szdtUnknown;
__attribute__((constructor))
static void initializer()
{
//we don't need a precise device determination, since the screen size in points really matters
//so, detect a particular device from the screen height
CGSize bounds = [UIScreen mainScreen].bounds.size;
switch ((int)MAX(bounds.width, bounds.height)) {
case 480:
currentDeviceType = szdtIPhone4s;
break;
case 568:
currentDeviceType = szdtIPhone5;
break;
case 667:
currentDeviceType = szdtIPhone6;
break;
case 736:
currentDeviceType = szdtIPhone6P;
break;
default:
break;
}
}
@dynamic constant;
-(CGFloat)constant {
return [self specificConstant];
}
-(void)setConstant:(CGFloat)constant {
//change specific constant proportionally to new value?
if (!self.ignoreChange && self.changeProportionally && _valuesSet[currentDeviceType]) {
switch (currentDeviceType) {
case szdtIPhone4s:
self.constant4s *= constant / [super constant];
case szdtIPhone5:
self.constant5 *= constant / [super constant];
case szdtIPhone6:
self.constant6 *= constant / [super constant];
case szdtIPhone6P:
self.constant6Plus *= constant / [super constant];
default:
;
}
} else {
//should new value be ignored?
if (!self.ignoreChange && _valuesSet[currentDeviceType]) {
switch (currentDeviceType) {
case szdtIPhone4s:
self.constant4s = constant;
case szdtIPhone5:
self.constant5 = constant;
case szdtIPhone6:
self.constant6 = constant;
case szdtIPhone6P:
self.constant6Plus = constant;
default:
;
}
}
}
[super setConstant:[self specificConstant]];
}
#pragma mark internal methods
-(CGFloat)specificConstant {
if (!_valuesSet[currentDeviceType]) {
return [super constant];
}
switch (currentDeviceType) {
case szdtIPhone4s:
return self.constant4s;
case szdtIPhone5:
return self.constant5;
case szdtIPhone6:
return self.constant6;
case szdtIPhone6P:
return self.constant6Plus;
case szdtUnknown:
return [super constant];
default:
return [super constant];
}
}
-(void)setConstant4s:(CGFloat)constant4s {
_constant4s = constant4s;
_valuesSet[szdtIPhone4s] = YES;
}
-(void)setConstant5:(CGFloat)constant5 {
_constant5 = constant5;
_valuesSet[szdtIPhone5] = YES;
}
-(void)setConstant6:(CGFloat)constant6 {
_constant6 = constant6;
_valuesSet[szdtIPhone6] = YES;
}
-(void)setConstant6Plus:(CGFloat)constant6Plus {
_constant6Plus = constant6Plus;
_valuesSet[szdtIPhone6P] = YES;
}
@end