Authored by Jonas Budelmann

Merge pull request #148 from kouky/aspect-fit-example

Aspect fit example
@@ -8,6 +8,7 @@ @@ -8,6 +8,7 @@
8 8
9 /* Begin PBXBuildFile section */ 9 /* Begin PBXBuildFile section */
10 114413091924B6EE008E702E /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 114413081924B6EE008E702E /* Default-568h@2x.png */; }; 10 114413091924B6EE008E702E /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 114413081924B6EE008E702E /* Default-568h@2x.png */; };
  11 + 27A27D461A6CF0C400D34F52 /* MASExampleAspectFitView.m in Sources */ = {isa = PBXBuildFile; fileRef = 27A27D451A6CF0C400D34F52 /* MASExampleAspectFitView.m */; };
11 3C02224919D0C4EC00507321 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3C02224819D0C4EC00507321 /* Images.xcassets */; }; 12 3C02224919D0C4EC00507321 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3C02224819D0C4EC00507321 /* Images.xcassets */; };
12 3DB1CAD5184538E200E91FC5 /* MASExampleArrayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */; }; 13 3DB1CAD5184538E200E91FC5 /* MASExampleArrayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */; };
13 4BEB55B61957394E008C862B /* MASExampleRemakeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BEB55B51957394E008C862B /* MASExampleRemakeView.m */; }; 14 4BEB55B61957394E008C862B /* MASExampleRemakeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BEB55B51957394E008C862B /* MASExampleRemakeView.m */; };
@@ -34,6 +35,8 @@ @@ -34,6 +35,8 @@
34 35
35 /* Begin PBXFileReference section */ 36 /* Begin PBXFileReference section */
36 114413081924B6EE008E702E /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; }; 37 114413081924B6EE008E702E /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
  38 + 27A27D441A6CF0C400D34F52 /* MASExampleAspectFitView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleAspectFitView.h; sourceTree = "<group>"; };
  39 + 27A27D451A6CF0C400D34F52 /* MASExampleAspectFitView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleAspectFitView.m; sourceTree = "<group>"; };
37 3C02224819D0C4EC00507321 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; }; 40 3C02224819D0C4EC00507321 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
38 3DB1CAD3184538E200E91FC5 /* MASExampleArrayView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleArrayView.h; sourceTree = "<group>"; }; 41 3DB1CAD3184538E200E91FC5 /* MASExampleArrayView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleArrayView.h; sourceTree = "<group>"; };
39 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleArrayView.m; sourceTree = "<group>"; }; 42 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleArrayView.m; sourceTree = "<group>"; };
@@ -155,6 +158,8 @@ @@ -155,6 +158,8 @@
155 DD52F24A179CADC0005CD195 /* MASExampleConstantsView.m */, 158 DD52F24A179CADC0005CD195 /* MASExampleConstantsView.m */,
156 DD52F24D179CADC0005CD195 /* MASExampleSidesView.h */, 159 DD52F24D179CADC0005CD195 /* MASExampleSidesView.h */,
157 DD52F24E179CADC0005CD195 /* MASExampleSidesView.m */, 160 DD52F24E179CADC0005CD195 /* MASExampleSidesView.m */,
  161 + 27A27D441A6CF0C400D34F52 /* MASExampleAspectFitView.h */,
  162 + 27A27D451A6CF0C400D34F52 /* MASExampleAspectFitView.m */,
158 DDF3875A179D648D00178773 /* MASExampleAnimatedView.h */, 163 DDF3875A179D648D00178773 /* MASExampleAnimatedView.h */,
159 DDF3875B179D648D00178773 /* MASExampleAnimatedView.m */, 164 DDF3875B179D648D00178773 /* MASExampleAnimatedView.m */,
160 DD7CC17417ACE990007A469E /* MASExampleDebuggingView.h */, 165 DD7CC17417ACE990007A469E /* MASExampleDebuggingView.h */,
@@ -295,6 +300,7 @@ @@ -295,6 +300,7 @@
295 DD52F251179CADC0005CD195 /* MASExampleBasicView.m in Sources */, 300 DD52F251179CADC0005CD195 /* MASExampleBasicView.m in Sources */,
296 DD653E4A1843E61500D1EC5A /* MASExampleLayoutGuideViewController.m in Sources */, 301 DD653E4A1843E61500D1EC5A /* MASExampleLayoutGuideViewController.m in Sources */,
297 DDDF60CC181915E300BF7B8B /* MASExampleLabelView.m in Sources */, 302 DDDF60CC181915E300BF7B8B /* MASExampleLabelView.m in Sources */,
  303 + 27A27D461A6CF0C400D34F52 /* MASExampleAspectFitView.m in Sources */,
298 DD52F252179CADC0005CD195 /* MASExampleConstantsView.m in Sources */, 304 DD52F252179CADC0005CD195 /* MASExampleConstantsView.m in Sources */,
299 DD52F253179CADC0005CD195 /* MASExampleListViewController.m in Sources */, 305 DD52F253179CADC0005CD195 /* MASExampleListViewController.m in Sources */,
300 DD52F254179CADC0005CD195 /* MASExampleSidesView.m in Sources */, 306 DD52F254179CADC0005CD195 /* MASExampleSidesView.m in Sources */,
  1 +//
  2 +// MASExampleAspectFitView.h
  3 +// Masonry iOS Examples
  4 +//
  5 +// Created by Michael Koukoullis on 19/01/2015.
  6 +// Copyright (c) 2015 Jonas Budelmann. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +
  11 +@interface MASExampleAspectFitView : UIView
  12 +
  13 +@end
  1 +//
  2 +// MASExampleAspectFitView.m
  3 +// Masonry iOS Examples
  4 +//
  5 +// Created by Michael Koukoullis on 19/01/2015.
  6 +// Copyright (c) 2015 Jonas Budelmann. All rights reserved.
  7 +//
  8 +
  9 +#import "MASExampleAspectFitView.h"
  10 +
  11 +@interface MASExampleAspectFitView ()
  12 +@property UIView *topView;
  13 +@property UIView *topInnerView;
  14 +@property UIView *bottomView;
  15 +@property UIView *bottomInnerView;
  16 +@end
  17 +
  18 +@implementation MASExampleAspectFitView
  19 +
  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];
  88 +}
  89 +
  90 +@end
@@ -20,6 +20,7 @@ @@ -20,6 +20,7 @@
20 #import "MASExampleLayoutGuideViewController.h" 20 #import "MASExampleLayoutGuideViewController.h"
21 #import "MASExampleArrayView.h" 21 #import "MASExampleArrayView.h"
22 #import "MASExampleAttributeChainingView.h" 22 #import "MASExampleAttributeChainingView.h"
  23 +#import "MASExampleAspectFitView.h"
23 24
24 static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier"; 25 static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
25 26
@@ -48,6 +49,8 @@ static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier"; @@ -48,6 +49,8 @@ static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
48 viewClass:MASExampleConstantsView.class], 49 viewClass:MASExampleConstantsView.class],
49 [[MASExampleViewController alloc] initWithTitle:@"Composite Edges" 50 [[MASExampleViewController alloc] initWithTitle:@"Composite Edges"
50 viewClass:MASExampleSidesView.class], 51 viewClass:MASExampleSidesView.class],
  52 + [[MASExampleViewController alloc] initWithTitle:@"Aspect Fit"
  53 + viewClass:MASExampleAspectFitView.class],
51 [[MASExampleViewController alloc] initWithTitle:@"Basic Animated" 54 [[MASExampleViewController alloc] initWithTitle:@"Basic Animated"
52 viewClass:MASExampleAnimatedView.class], 55 viewClass:MASExampleAnimatedView.class],
53 [[MASExampleViewController alloc] initWithTitle:@"Debugging Helpers" 56 [[MASExampleViewController alloc] initWithTitle:@"Debugging Helpers"