-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearLayout.m
93 lines (68 loc) · 2.54 KB
/
LinearLayout.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
#import "LinearLayout.h"
#import "AutolayoutHelper.h"
#define DEFAULT_VIEW_SIZE NSIntegerMax
@interface LinearLayout ()
@property(nonatomic, strong) AutolayoutHelper* layout;
@property(nonatomic) NSUInteger numberOfViews;
@end
@implementation LinearLayout
- (instancetype)initVertical {
return [self initWithOrientation:UILayoutConstraintAxisVertical];
}
- (instancetype)initHorizontal {
return [self initWithOrientation:UILayoutConstraintAxisHorizontal];
}
- (instancetype)initWithOrientation:(UILayoutConstraintAxis)orientation
{
self = [super init];
_orientation = orientation;
self.numberOfViews = 0;
[self setAllMargins:0];
self.layout = [[AutolayoutHelper alloc] initWithView:self];
return self;
}
- (void)setAllMargins:(CGFloat)margin
{
self.marginEnds = self.marginBetween = self.marginSides = margin;
}
- (void)appendSubview:(UIView*)view
{
[self appendSubview:view size:DEFAULT_VIEW_SIZE];
}
- (void)appendSubview:(UIView*)view size:(CGFloat)size
{
self.layout.metrics = @{
@"e":@(self.marginEnds),
@"b":@(self.marginBetween),
@"l":@(self.marginSides),
@"s":@(size)
};
NSUInteger i = ++self.numberOfViews;
NSString* viewKey = [self viewKeyForIndex:i];
[self.layout addView:view withKey:viewKey];
if (size != DEFAULT_VIEW_SIZE) {
[self.layout addConstraint:[NSString stringWithFormat:@"%@:[%@(s)]", [self mainType], viewKey]];
}
if (i == 1) {
// For first view, align to superview start
[self.layout addConstraint:[NSString stringWithFormat:@"%@:|-(e)-[%@]", [self mainType], viewKey]];
} else {
// Align to previous view
NSString* previousViewKey = [self viewKeyForIndex:i-1];
[self.layout addConstraint:[NSString stringWithFormat:@"%@:[%@]-(b)-[%@]", [self mainType], previousViewKey, viewKey]];
}
// Now align to superview end (override constraint)
[self.layout setConstraints:@[[NSString stringWithFormat:@"%@:[%@]-(e)-|", [self mainType], viewKey]] forKey:@"end align"];
// And align to superview margins in the other orientation
[self.layout addConstraint:[NSString stringWithFormat:@"%@:|-(l)-[%@]-(l)-|", [self otherType], viewKey]];
}
- (NSString*)viewKeyForIndex:(NSUInteger)i {
return [NSString stringWithFormat:@"v%lu", (unsigned long)i];
}
- (NSString*)mainType {
return self.orientation == UILayoutConstraintAxisHorizontal ? @"H" : @"V";
}
- (NSString*)otherType {
return self.orientation == UILayoutConstraintAxisHorizontal ? @"V" : @"H";
}
@end