From 1a7f0d235fc438cda37d03b2964475b90a0f76f0 Mon Sep 17 00:00:00 2001 From: Craig Siemens Date: Mon, 23 Feb 2015 10:34:41 -0700 Subject: [PATCH 1/2] Added the iOS 8 margin attributes --- Masonry/MASConstraint.h | 14 +++++ Masonry/MASConstraint.m | 36 +++++++++++ Masonry/MASConstraintMaker.h | 27 +++++++++ Masonry/MASConstraintMaker.m | 59 ++++++++++++++++++- .../NSLayoutConstraint+MASDebugAdditions.m | 12 ++++ Masonry/View+MASAdditions.h | 13 ++++ Masonry/View+MASAdditions.m | 36 +++++++++++ Masonry/View+MASShorthandAdditions.h | 26 ++++++++ 8 files changed, 222 insertions(+), 1 deletion(-) diff --git a/Masonry/MASConstraint.h b/Masonry/MASConstraint.h index 648b7815..a4b7a781 100644 --- a/Masonry/MASConstraint.h +++ b/Masonry/MASConstraint.h @@ -127,6 +127,20 @@ - (MASConstraint *)centerY; - (MASConstraint *)baseline; +#if TARGET_OS_IPHONE + +- (MASConstraint *)leftMargin; +- (MASConstraint *)rightMargin; +- (MASConstraint *)topMargin; +- (MASConstraint *)bottomMargin; +- (MASConstraint *)leadingMargin; +- (MASConstraint *)trailingMargin; +- (MASConstraint *)centerXWithinMargins; +- (MASConstraint *)centerYWithinMargins; + +#endif + + /** * Sets the constraint debug name */ diff --git a/Masonry/MASConstraint.m b/Masonry/MASConstraint.m index 78ce96a2..8497912e 100644 --- a/Masonry/MASConstraint.m +++ b/Masonry/MASConstraint.m @@ -208,6 +208,42 @@ - (MASConstraint *)baseline { return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; } +#if TARGET_OS_IPHONE + +- (MASConstraint *)leftMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; +} + +- (MASConstraint *)rightMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; +} + +- (MASConstraint *)topMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; +} + +- (MASConstraint *)bottomMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; +} + +- (MASConstraint *)leadingMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; +} + +- (MASConstraint *)trailingMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; +} + +- (MASConstraint *)centerXWithinMargins { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; +} + +- (MASConstraint *)centerYWithinMargins { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; +} + +#endif + #pragma mark - Abstract - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); } diff --git a/Masonry/MASConstraintMaker.h b/Masonry/MASConstraintMaker.h index d8e7fc25..7e318585 100644 --- a/Masonry/MASConstraintMaker.h +++ b/Masonry/MASConstraintMaker.h @@ -21,6 +21,20 @@ typedef NS_OPTIONS(NSInteger, MASAttribute) { MASAttributeCenterX = 1 << NSLayoutAttributeCenterX, MASAttributeCenterY = 1 << NSLayoutAttributeCenterY, MASAttributeBaseline = 1 << NSLayoutAttributeBaseline, + +#if TARGET_OS_IPHONE + + MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin, + MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin, + MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin, + MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin, + MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin, + MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin, + MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins, + MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins, + +#endif + }; /** @@ -46,6 +60,19 @@ typedef NS_OPTIONS(NSInteger, MASAttribute) { @property (nonatomic, strong, readonly) MASConstraint *centerY; @property (nonatomic, strong, readonly) MASConstraint *baseline; +#if TARGET_OS_IPHONE + +@property (nonatomic, strong, readonly) MASConstraint *leftMargin; +@property (nonatomic, strong, readonly) MASConstraint *rightMargin; +@property (nonatomic, strong, readonly) MASConstraint *topMargin; +@property (nonatomic, strong, readonly) MASConstraint *bottomMargin; +@property (nonatomic, strong, readonly) MASConstraint *leadingMargin; +@property (nonatomic, strong, readonly) MASConstraint *trailingMargin; +@property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins; +@property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins; + +#endif + /** * Returns a block which creates a new MASCompositeConstraint with the first item set * to the makers associated view and children corresponding to the set bits in the diff --git a/Masonry/MASConstraintMaker.m b/Masonry/MASConstraintMaker.m index b7206e0b..78f93ec5 100644 --- a/Masonry/MASConstraintMaker.m +++ b/Masonry/MASConstraintMaker.m @@ -75,7 +75,15 @@ - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayou } - (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs { - __unused MASAttribute anyAttribute = MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX | MASAttributeCenterY | MASAttributeBaseline; + __unused MASAttribute anyAttribute = (MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading + | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX + | MASAttributeCenterY | MASAttributeBaseline +#if TARGET_OS_IPHONE + | MASAttributeLeftMargin | MASAttributeRightMargin | MASAttributeTopMargin | MASAttributeBottomMargin + | MASAttributeLeadingMargin | MASAttributeTrailingMargin | MASAttributeCenterXWithinMargins + | MASAttributeCenterYWithinMargins +#endif + ); NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)"); @@ -93,6 +101,19 @@ - (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs { if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY]; if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline]; +#if TARGET_OS_IPHONE + + if (attrs & MASAttributeLeftMargin) [attributes addObject:self.view.mas_leftMargin]; + if (attrs & MASAttributeRightMargin) [attributes addObject:self.view.mas_rightMargin]; + if (attrs & MASAttributeTopMargin) [attributes addObject:self.view.mas_topMargin]; + if (attrs & MASAttributeBottomMargin) [attributes addObject:self.view.mas_bottomMargin]; + if (attrs & MASAttributeLeadingMargin) [attributes addObject:self.view.mas_leadingMargin]; + if (attrs & MASAttributeTrailingMargin) [attributes addObject:self.view.mas_trailingMargin]; + if (attrs & MASAttributeCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins]; + if (attrs & MASAttributeCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins]; + +#endif + NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count]; for (MASViewAttribute *a in attributes) { @@ -161,6 +182,42 @@ - (MASConstraint *)baseline { }; } +#if TARGET_OS_IPHONE + +- (MASConstraint *)leftMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; +} + +- (MASConstraint *)rightMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; +} + +- (MASConstraint *)topMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; +} + +- (MASConstraint *)bottomMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; +} + +- (MASConstraint *)leadingMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; +} + +- (MASConstraint *)trailingMargin { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; +} + +- (MASConstraint *)centerXWithinMargins { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; +} + +- (MASConstraint *)centerYWithinMargins { + return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; +} + +#endif + #pragma mark - composite Attributes diff --git a/Masonry/NSLayoutConstraint+MASDebugAdditions.m b/Masonry/NSLayoutConstraint+MASDebugAdditions.m index 674943fe..3d10ac2b 100644 --- a/Masonry/NSLayoutConstraint+MASDebugAdditions.m +++ b/Masonry/NSLayoutConstraint+MASDebugAdditions.m @@ -43,6 +43,18 @@ + (NSDictionary *)layoutAttributeDescriptionsByValue { @(NSLayoutAttributeCenterX) : @"centerX", @(NSLayoutAttributeCenterY) : @"centerY", @(NSLayoutAttributeBaseline) : @"baseline", + +#if TARGET_OS_IPHONE + @(NSLayoutAttributeLeftMargin) : @"leftMargin", + @(NSLayoutAttributeRightMargin) : @"rightMargin", + @(NSLayoutAttributeTopMargin) : @"topMargin", + @(NSLayoutAttributeBottomMargin) : @"bottomMargin", + @(NSLayoutAttributeLeadingMargin) : @"leadingMargin", + @(NSLayoutAttributeTrailingMargin) : @"trailingMargin", + @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins", + @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins", +#endif + }; }); diff --git a/Masonry/View+MASAdditions.h b/Masonry/View+MASAdditions.h index 7c29a0f3..94e30504 100644 --- a/Masonry/View+MASAdditions.h +++ b/Masonry/View+MASAdditions.h @@ -32,6 +32,19 @@ @property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline; @property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr); +#if TARGET_OS_IPHONE + +@property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; +@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; + +#endif + /** * a key to associate with this view */ diff --git a/Masonry/View+MASAdditions.m b/Masonry/View+MASAdditions.m index d91d9fd8..acfaa527 100644 --- a/Masonry/View+MASAdditions.m +++ b/Masonry/View+MASAdditions.m @@ -87,6 +87,42 @@ - (MASViewAttribute *)mas_baseline { }; } +#if TARGET_OS_IPHONE + +- (MASViewAttribute *)mas_leftMargin { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin]; +} + +- (MASViewAttribute *)mas_rightMargin { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin]; +} + +- (MASViewAttribute *)mas_topMargin { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin]; +} + +- (MASViewAttribute *)mas_bottomMargin { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin]; +} + +- (MASViewAttribute *)mas_leadingMargin { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin]; +} + +- (MASViewAttribute *)mas_trailingMargin { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin]; +} + +- (MASViewAttribute *)mas_centerXWithinMargins { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins]; +} + +- (MASViewAttribute *)mas_centerYWithinMargins { + return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins]; +} + +#endif + #pragma mark - associated properties - (id)mas_key { diff --git a/Masonry/View+MASShorthandAdditions.h b/Masonry/View+MASShorthandAdditions.h index 90033944..55539124 100644 --- a/Masonry/View+MASShorthandAdditions.h +++ b/Masonry/View+MASShorthandAdditions.h @@ -29,6 +29,19 @@ @property (nonatomic, strong, readonly) MASViewAttribute *baseline; @property (nonatomic, strong, readonly) MASViewAttribute *(^attribute)(NSLayoutAttribute attr); +#if TARGET_OS_IPHONE + +@property (nonatomic, strong, readonly) MASViewAttribute *leftMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *rightMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *topMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *bottomMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *leadingMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *trailingMargin; +@property (nonatomic, strong, readonly) MASViewAttribute *centerXWithinMargins; +@property (nonatomic, strong, readonly) MASViewAttribute *centerYWithinMargins; + +#endif + - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block; - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block; - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block; @@ -54,6 +67,19 @@ MAS_ATTR_FORWARD(centerX); MAS_ATTR_FORWARD(centerY); MAS_ATTR_FORWARD(baseline); +#if TARGET_OS_IPHONE + +MAS_ATTR_FORWARD(leftMargin); +MAS_ATTR_FORWARD(rightMargin); +MAS_ATTR_FORWARD(topMargin); +MAS_ATTR_FORWARD(bottomMargin); +MAS_ATTR_FORWARD(leadingMargin); +MAS_ATTR_FORWARD(trailingMargin); +MAS_ATTR_FORWARD(centerXWithinMargins); +MAS_ATTR_FORWARD(centerYWithinMargins); + +#endif + - (MASViewAttribute *(^)(NSLayoutAttribute))attribute { return [self mas_attribute]; } From 380b75d85da70cb34378011b05d211963359d891 Mon Sep 17 00:00:00 2001 From: Craig Siemens Date: Mon, 23 Feb 2015 10:58:14 -0700 Subject: [PATCH 2/2] Added an example view using the margin layout attributes --- .../project.pbxproj | 6 +++ .../MASExampleListViewController.m | 4 ++ .../MASExampleMarginView.h | 13 ++++++ .../MASExampleMarginView.m | 46 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 Examples/Masonry iOS Examples/MASExampleMarginView.h create mode 100644 Examples/Masonry iOS Examples/MASExampleMarginView.m diff --git a/Examples/Masonry iOS Examples.xcodeproj/project.pbxproj b/Examples/Masonry iOS Examples.xcodeproj/project.pbxproj index 5b691ddb..89049aa8 100644 --- a/Examples/Masonry iOS Examples.xcodeproj/project.pbxproj +++ b/Examples/Masonry iOS Examples.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ 27A27D461A6CF0C400D34F52 /* MASExampleAspectFitView.m in Sources */ = {isa = PBXBuildFile; fileRef = 27A27D451A6CF0C400D34F52 /* MASExampleAspectFitView.m */; }; 3C02224919D0C4EC00507321 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3C02224819D0C4EC00507321 /* Images.xcassets */; }; 3DB1CAD5184538E200E91FC5 /* MASExampleArrayView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */; }; + 44C0E6AF1A9B9C55003C70CF /* MASExampleMarginView.m in Sources */ = {isa = PBXBuildFile; fileRef = 44C0E6AE1A9B9C55003C70CF /* MASExampleMarginView.m */; }; 4BEB55B61957394E008C862B /* MASExampleRemakeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BEB55B51957394E008C862B /* MASExampleRemakeView.m */; }; 6C87DADA5AB046D9A3181A65 /* libPods-Masonry iOS Examples.a in Frameworks */ = {isa = PBXBuildFile; fileRef = BDC1B8303EED42A2B01B94B1 /* libPods-Masonry iOS Examples.a */; }; DD175E6A182639FB0099129A /* MASExampleUpdateView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD175E69182639FB0099129A /* MASExampleUpdateView.m */; }; @@ -40,6 +41,8 @@ 3C02224819D0C4EC00507321 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 3DB1CAD3184538E200E91FC5 /* MASExampleArrayView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleArrayView.h; sourceTree = ""; }; 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleArrayView.m; sourceTree = ""; }; + 44C0E6AD1A9B9C55003C70CF /* MASExampleMarginView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleMarginView.h; sourceTree = ""; }; + 44C0E6AE1A9B9C55003C70CF /* MASExampleMarginView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleMarginView.m; sourceTree = ""; }; 4BEB55B41957394E008C862B /* MASExampleRemakeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleRemakeView.h; sourceTree = ""; }; 4BEB55B51957394E008C862B /* MASExampleRemakeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleRemakeView.m; sourceTree = ""; }; B086DD7D31DD4B49ADC08504 /* Pods-Masonry iOS Examples.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Masonry iOS Examples.xcconfig"; path = "../Pods/Pods-Masonry iOS Examples.xcconfig"; sourceTree = ""; }; @@ -176,6 +179,8 @@ 3DB1CAD4184538E200E91FC5 /* MASExampleArrayView.m */, DD32C3FB18E8BFF6001F6AD2 /* MASExampleAttributeChainingView.h */, DD32C3FC18E8BFF6001F6AD2 /* MASExampleAttributeChainingView.m */, + 44C0E6AD1A9B9C55003C70CF /* MASExampleMarginView.h */, + 44C0E6AE1A9B9C55003C70CF /* MASExampleMarginView.m */, ); name = Views; sourceTree = ""; @@ -298,6 +303,7 @@ 3DB1CAD5184538E200E91FC5 /* MASExampleArrayView.m in Sources */, DD52F23B179CAD57005CD195 /* MASAppDelegate.m in Sources */, DD52F251179CADC0005CD195 /* MASExampleBasicView.m in Sources */, + 44C0E6AF1A9B9C55003C70CF /* MASExampleMarginView.m in Sources */, DD653E4A1843E61500D1EC5A /* MASExampleLayoutGuideViewController.m in Sources */, DDDF60CC181915E300BF7B8B /* MASExampleLabelView.m in Sources */, 27A27D461A6CF0C400D34F52 /* MASExampleAspectFitView.m in Sources */, diff --git a/Examples/Masonry iOS Examples/MASExampleListViewController.m b/Examples/Masonry iOS Examples/MASExampleListViewController.m index 506314a2..bd1239c0 100644 --- a/Examples/Masonry iOS Examples/MASExampleListViewController.m +++ b/Examples/Masonry iOS Examples/MASExampleListViewController.m @@ -21,6 +21,7 @@ #import "MASExampleArrayView.h" #import "MASExampleAttributeChainingView.h" #import "MASExampleAspectFitView.h" +#import "MASExampleMarginView.h" static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier"; @@ -63,6 +64,9 @@ - (id)init { viewClass:MASExampleArrayView.class], [[MASExampleViewController alloc] initWithTitle:@"Attribute Chaining" viewClass:MASExampleAttributeChainingView.class], + [[MASExampleViewController alloc] initWithTitle:@"Margins" + viewClass:MASExampleMarginView.class], + ]; if ([UIViewController instancesRespondToSelector:@selector(topLayoutGuide)]) diff --git a/Examples/Masonry iOS Examples/MASExampleMarginView.h b/Examples/Masonry iOS Examples/MASExampleMarginView.h new file mode 100644 index 00000000..7c0e14e3 --- /dev/null +++ b/Examples/Masonry iOS Examples/MASExampleMarginView.h @@ -0,0 +1,13 @@ +// +// MASExampleMarginView.h +// Masonry iOS Examples +// +// Created by Craig Siemens on 2015-02-23. +// Copyright (c) 2015 Jonas Budelmann. All rights reserved. +// + +#import + +@interface MASExampleMarginView : UIView + +@end diff --git a/Examples/Masonry iOS Examples/MASExampleMarginView.m b/Examples/Masonry iOS Examples/MASExampleMarginView.m new file mode 100644 index 00000000..2dfcb6f8 --- /dev/null +++ b/Examples/Masonry iOS Examples/MASExampleMarginView.m @@ -0,0 +1,46 @@ +// +// MASExampleMarginView.m +// Masonry iOS Examples +// +// Created by Craig Siemens on 2015-02-23. +// Copyright (c) 2015 Jonas Budelmann. All rights reserved. +// + +#import "MASExampleMarginView.h" + +@implementation MASExampleMarginView + +- (instancetype)init { + self = [super init]; + if (!self) return nil; + + UIView *lastView = self; + for (int i = 0; i < 10; i++) { + UIView *view = UIView.new; + view.backgroundColor = [self randomColor]; + view.layer.borderColor = UIColor.blackColor.CGColor; + view.layer.borderWidth = 2; + view.layoutMargins = UIEdgeInsetsMake(5, 10, 15, 20); + [self addSubview:view]; + + [view mas_makeConstraints:^(MASConstraintMaker *make) { + make.top.equalTo(lastView.topMargin); + make.bottom.equalTo(lastView.bottomMargin); + make.left.equalTo(lastView.leftMargin); + make.right.equalTo(lastView.rightMargin); + }]; + + lastView = view; + } + + return self; +} + +- (UIColor *)randomColor { + CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0 + CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white + CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black + return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; +} + +@end