Configure views and layout for aspect fit example
Showing
1 changed file
with
75 additions
and
6 deletions
@@ -8,14 +8,83 @@ | @@ -8,14 +8,83 @@ | ||
8 | 8 | ||
9 | #import "MASExampleAspectFitView.h" | 9 | #import "MASExampleAspectFitView.h" |
10 | 10 | ||
11 | +@interface MASExampleAspectFitView () | ||
12 | +@property UIView *topView; | ||
13 | +@property UIView *topInnerView; | ||
14 | +@property UIView *bottomView; | ||
15 | +@property UIView *bottomInnerView; | ||
16 | +@end | ||
17 | + | ||
11 | @implementation MASExampleAspectFitView | 18 | @implementation MASExampleAspectFitView |
12 | 19 | ||
13 | -/* | ||
14 | -// Only override drawRect: if you perform custom drawing. | ||
15 | -// An empty implementation adversely affects performance during animation. | ||
16 | -- (void)drawRect:(CGRect)rect { | ||
17 | - // Drawing code | 20 | +// Designated initializer |
21 | +- (instancetype)init | ||
22 | +{ | ||
23 | + self = [super initWithFrame:CGRectZero]; | ||
24 | + | ||
25 | + if (self) { | ||
26 | + | ||
27 | + // Create views | ||
28 | + self.topView = [[UIView alloc] initWithFrame:CGRectZero]; | ||
29 | + self.topInnerView = [[UIView alloc] initWithFrame:CGRectZero]; | ||
30 | + self.bottomView = [[UIView alloc] initWithFrame:CGRectZero]; | ||
31 | + self.bottomInnerView = [[UIView alloc] initWithFrame:CGRectZero]; | ||
32 | + | ||
33 | + // Set background colors | ||
34 | + UIColor *blueColor = [UIColor colorWithRed:0.663 green:0.796 blue:0.996 alpha:1]; | ||
35 | + [self.topView setBackgroundColor:blueColor]; | ||
36 | + | ||
37 | + UIColor *lightGreenColor = [UIColor colorWithRed:0.784 green:0.992 blue:0.851 alpha:1]; | ||
38 | + [self.topInnerView setBackgroundColor:lightGreenColor]; | ||
39 | + | ||
40 | + UIColor *pinkColor = [UIColor colorWithRed:0.992 green:0.804 blue:0.941 alpha:1]; | ||
41 | + [self.bottomView setBackgroundColor:pinkColor]; | ||
42 | + | ||
43 | + UIColor *darkGreenColor = [UIColor colorWithRed:0.443 green:0.780 blue:0.337 alpha:1]; | ||
44 | + [self.bottomInnerView setBackgroundColor:darkGreenColor]; | ||
45 | + | ||
46 | + // Layout top and bottom views to each take up half of the window | ||
47 | + [self addSubview:self.topView]; | ||
48 | + [self.topView mas_makeConstraints:^(MASConstraintMaker *make) { | ||
49 | + make.left.right.and.top.equalTo(self); | ||
50 | + }]; | ||
51 | + | ||
52 | + [self addSubview:self.bottomView]; | ||
53 | + [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) { | ||
54 | + make.left.right.and.bottom.equalTo(self); | ||
55 | + make.top.equalTo(self.topView.mas_bottom); | ||
56 | + make.height.equalTo(self.topView); | ||
57 | + }]; | ||
58 | + | ||
59 | + // Inner views are configured for aspect fit with ratio of 3:1 | ||
60 | + [self.topView addSubview:self.topInnerView]; | ||
61 | + [self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) { | ||
62 | + make.width.equalTo(self.topInnerView.mas_height).multipliedBy(3); | ||
63 | + | ||
64 | + make.width.and.height.lessThanOrEqualTo(self.topView); | ||
65 | + make.width.and.height.equalTo(self.topView).with.priorityLow(); | ||
66 | + | ||
67 | + make.center.equalTo(self.topView); | ||
68 | + }]; | ||
69 | + | ||
70 | + [self.bottomView addSubview:self.bottomInnerView]; | ||
71 | + [self.bottomInnerView mas_makeConstraints:^(MASConstraintMaker *make) { | ||
72 | + make.height.equalTo(self.bottomInnerView.mas_width).multipliedBy(3); | ||
73 | + | ||
74 | + make.width.and.height.lessThanOrEqualTo(self.bottomView); | ||
75 | + make.width.and.height.equalTo(self.bottomView).with.priorityLow(); | ||
76 | + | ||
77 | + make.center.equalTo(self.bottomView); | ||
78 | + }]; | ||
79 | + } | ||
80 | + | ||
81 | + return self; | ||
82 | +} | ||
83 | + | ||
84 | +// Override previous designated initializer | ||
85 | +- (instancetype)initWithFrame:(CGRect)frame | ||
86 | +{ | ||
87 | + return [self init]; | ||
18 | } | 88 | } |
19 | -*/ | ||
20 | 89 | ||
21 | @end | 90 | @end |
-
Please register or login to post a comment