forked from shaojiankui/JKCategories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+FDCollapsibleConstraints.m
180 lines (146 loc) · 6.06 KB
/
UIView+FDCollapsibleConstraints.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// The MIT License (MIT)
//
// Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import "UIView+FDCollapsibleConstraints.h"
#import <objc/runtime.h>
/// A stored property extension for NSLayoutConstraint's original constant.
@implementation NSLayoutConstraint (_FDOriginalConstantStorage)
- (void)setFd_originalConstant:(CGFloat)originalConstant
{
objc_setAssociatedObject(self, @selector(fd_originalConstant), @(originalConstant), OBJC_ASSOCIATION_RETAIN);
}
- (CGFloat)fd_originalConstant
{
#if CGFLOAT_IS_DOUBLE
return [objc_getAssociatedObject(self, _cmd) doubleValue];
#else
return [objc_getAssociatedObject(self, _cmd) floatValue];
#endif
}
@end
@implementation UIView (FDCollapsibleConstraints)
#pragma mark - Hacking KVC
+ (void)load
{
// Swizzle setValue:forKey: to intercept assignments to `fd_collapsibleConstraints`
// from Interface Builder. We should not do so by overriding setvalue:forKey:
// as the primary class implementation would be bypassed.
SEL originalSelector = @selector(setValue:forKey:);
SEL swizzledSelector = @selector(fd_setValue:forKey:);
Class class = UIView.class;
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
- (void)fd_setValue:(id)value forKey:(NSString *)key
{
NSString *injectedKey = [NSString stringWithUTF8String:sel_getName(@selector(fd_collapsibleConstraints))];
if ([key isEqualToString:injectedKey]) {
// This kind of IBOutlet won't trigger property's setter, so we forward it.
self.fd_collapsibleConstraints = value;
} else {
// Forward the rest of KVC's to original implementation.
[self fd_setValue:value forKey:key];
}
}
#pragma mark - Dynamic Properties
- (void)setFd_collapsed:(BOOL)collapsed
{
[self.fd_collapsibleConstraints enumerateObjectsUsingBlock:
^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if (collapsed) {
constraint.constant = 0;
} else {
constraint.constant = constraint.fd_originalConstant;
}
}];
objc_setAssociatedObject(self, @selector(fd_collapsed), @(collapsed), OBJC_ASSOCIATION_RETAIN);
}
- (BOOL)fd_collapsed
{
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (NSMutableArray *)fd_collapsibleConstraints
{
NSMutableArray *constraints = objc_getAssociatedObject(self, _cmd);
if (!constraints) {
constraints = @[].mutableCopy;
objc_setAssociatedObject(self, _cmd, constraints, OBJC_ASSOCIATION_RETAIN);
}
return constraints;
}
- (void)setFd_collapsibleConstraints:(NSArray *)fd_collapsibleConstraints
{
// Hook assignments to our custom `fd_collapsibleConstraints` property.
NSMutableArray *constraints = (NSMutableArray *)self.fd_collapsibleConstraints;
[fd_collapsibleConstraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
// Store original constant value
constraint.fd_originalConstant = constraint.constant;
[constraints addObject:constraint];
}];
}
@end
@implementation UIView (FDAutomaticallyCollapseByIntrinsicContentSize)
#pragma mark - Hacking "-updateConstraints"
+ (void)load
{
// Swizzle to hack "-updateConstraints" method
SEL originalSelector = @selector(updateConstraints);
SEL swizzledSelector = @selector(fd_updateConstraints);
Class class = UIView.class;
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
method_exchangeImplementations(originalMethod, swizzledMethod);
}
- (void)fd_updateConstraints
{
// Call primary method's implementation
[self fd_updateConstraints];
if (self.fd_autoCollapse && self.fd_collapsibleConstraints.count > 0) {
// "Absent" means this view doesn't have an intrinsic content size, {-1, -1} actually.
const CGSize absentIntrinsicContentSize = CGSizeMake(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric);
// Calculated intrinsic content size
const CGSize contentSize = [self intrinsicContentSize];
// When this view doesn't have one, or has no intrinsic content size after calculating,
// it going to be collapsed.
if (CGSizeEqualToSize(contentSize, absentIntrinsicContentSize) ||
CGSizeEqualToSize(contentSize, CGSizeZero)) {
self.fd_collapsed = YES;
} else {
self.fd_collapsed = NO;
}
}
}
#pragma mark - Dynamic Properties
- (BOOL)fd_autoCollapse
{
return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setFd_autoCollapse:(BOOL)autoCollapse
{
objc_setAssociatedObject(self, @selector(fd_autoCollapse), @(autoCollapse), OBJC_ASSOCIATION_RETAIN);
}
- (void)setAutoCollapse:(BOOL)collapse
{
// Just forwarding
self.fd_autoCollapse = collapse;
}
@end