-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUIView+Barefoot.m
142 lines (125 loc) · 4.2 KB
/
UIView+Barefoot.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
#import "UIView+Barefoot.h"
#import <QuartzCore/CALayer.h>
@implementation UIView (Barefoot)
- (CGSize)subviewBounds
{
// Semi-cheap hack: if we are measuring a UIScrollView, it also has two UIImageView children
// for the scrollbars which must be disabled and then re-enabled after calculating the bounds.
UIScrollView *scrollView = nil;
BOOL showsHorizontalScrollIndicator = NO;
BOOL showsVerticalScrollIndicator = NO;
if([self isKindOfClass:[UIScrollView class]]) {
scrollView = (UIScrollView *)self;
showsHorizontalScrollIndicator = scrollView.showsHorizontalScrollIndicator;
showsVerticalScrollIndicator = scrollView.showsVerticalScrollIndicator;
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
}
CGSize bounds = CGSizeZero;
for(UIView *subView in self.subviews)
{
CGRect f = subView.frame;
bounds.width = MAX(bounds.width, f.origin.x + f.size.width);
bounds.height = MAX(bounds.height, f.origin.y + f.size.height);
}
if([self isKindOfClass:[UIScrollView class]]) {
[scrollView setShowsHorizontalScrollIndicator:showsHorizontalScrollIndicator];
[scrollView setShowsVerticalScrollIndicator:showsVerticalScrollIndicator];
}
return bounds;
}
- (void)addSubviewAtBottom:(UIView *)view
{
[self addSubviewAtBottom:view withPadding:0];
}
- (void)addSubviewAtRight:(UIView *)view
{
CGSize bounds = [self subviewBounds];
CGRect frame = view.frame;
frame.origin.x = bounds.width;
view.frame = frame;
[self addSubviewAtRight:view withPadding:0];
}
- (void)addSubview:(UIView *)view atRightOf:(UIView *)otherView
{
[self addSubview:view atRightOf:otherView withOffset:0];
}
- (void)addSubview:(UIView *)view atRightOf:(UIView *)otherView withOffset:(size_t)offset
{
CGRect frame = view.frame;
frame.origin.x = otherView.frame.origin.x + otherView.frame.size.width + offset;
frame.origin.y = otherView.frame.origin.y;
view.frame = frame;
[self addSubview:view];
}
- (void)addSubviewAtBottom:(UIView *)view withPadding:(size_t)padding
{
CGSize bounds = [self subviewBounds];
CGRect frame = view.frame;
frame.origin.y = bounds.height + padding;
view.frame = frame;
[self addSubview:view];
}
- (void)addSubviewAtRight:(UIView *)view withPadding:(size_t)padding
{
CGSize bounds = [self subviewBounds];
CGRect frame = view.frame;
frame.origin.x = bounds.width + padding;
view.frame = frame;
[self addSubview:view];
}
- (void)insertSubview:(UIView *)view beneath:(UIView *)otherView {
[self insertSubview:view beneath:otherView withTopOffset:0 bottomOffset:0];
}
- (void)insertSubview:(UIView *)view beneath:(UIView *)otherView withTopOffset:(CGFloat)topOffset bottomOffset:(CGFloat)bottomOffset {
CGRect frame;
for (UIView *subview in self.subviews) {
frame = subview.frame;
if (frame.origin.y >= otherView.frame.origin.y + otherView.frame.size.height) {
frame.origin.y += view.frame.size.height + topOffset + bottomOffset;
subview.frame = frame;
}
}
frame = view.frame;
frame.origin.y = otherView.frame.origin.y + otherView.frame.size.height + topOffset;
view.frame = frame;
[self addSubview:view];
}
- (void)sizeToFitWithPadding:(CGSize)padding
{
CGSize bounds = [self subviewBounds];
CGRect frame = self.frame;
frame.size.height = bounds.height + padding.height;
frame.size.width = bounds.width + padding.width;
self.frame = frame;
}
- (void)sizeToFitWithPadding:(CGSize)padding withMinimumSize:(CGSize)minSize
{
CGSize bounds = [self subviewBounds];
CGRect frame = self.frame;
frame.size.height = MAX(bounds.height + padding.height, minSize.height);
frame.size.width = MAX(bounds.width + padding.width, minSize.width);
self.frame = frame;
}
- (void)sizeHeightToFitWithPadding:(size_t)h
{
CGSize bounds = [self subviewBounds];
CGRect frame = self.frame;
frame.size.height = bounds.height + h;
self.frame = frame;
}
- (void)describeWithSpaces:(NSUInteger)spaces {
NSString *s = @"";
for (NSUInteger i = 0; i <= spaces; i++) {
s = [s stringByAppendingString:@" "];
}
NSLog(@"%@%@", s, self);
spaces += 2;
for (UIView *subview in self.subviews) {
[subview describeWithSpaces:spaces];
}
}
- (void)describe {
[self describeWithSpaces:0];
}
@end