Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add UI/NSLayoutGuide support like UIView #585

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Examples/Masonry iOS Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
00FC4A321B7359D700DCA999 /* MASExampleDistributeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 00FC4A311B7359D700DCA999 /* MASExampleDistributeView.m */; };
106A947E24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.m in Sources */ = {isa = PBXBuildFile; fileRef = 106A947D24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.m */; };
114413091924B6EE008E702E /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 114413081924B6EE008E702E /* [email protected] */; };
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 */; };
Expand Down Expand Up @@ -39,6 +41,8 @@
/* Begin PBXFileReference section */
00FC4A301B7359D700DCA999 /* MASExampleDistributeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleDistributeView.h; sourceTree = "<group>"; };
00FC4A311B7359D700DCA999 /* MASExampleDistributeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleDistributeView.m; sourceTree = "<group>"; };
106A947C24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MASExampleLayoutGuideView.h; sourceTree = "<group>"; };
106A947D24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MASExampleLayoutGuideView.m; sourceTree = "<group>"; };
27A27D441A6CF0C400D34F52 /* MASExampleAspectFitView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleAspectFitView.h; sourceTree = "<group>"; };
27A27D451A6CF0C400D34F52 /* MASExampleAspectFitView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleAspectFitView.m; sourceTree = "<group>"; };
321AA59CF7B045B6D503D2E5 /* Pods-Masonry iOS Examples.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Masonry iOS Examples.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Masonry iOS Examples/Pods-Masonry iOS Examples.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -190,6 +194,8 @@
44C0E6AE1A9B9C55003C70CF /* MASExampleMarginView.m */,
00FC4A301B7359D700DCA999 /* MASExampleDistributeView.h */,
00FC4A311B7359D700DCA999 /* MASExampleDistributeView.m */,
106A947C24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.h */,
106A947D24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.m */,
);
name = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -343,6 +349,7 @@
3DB1CAD5184538E200E91FC5 /* MASExampleArrayView.m in Sources */,
DD52F23B179CAD57005CD195 /* MASAppDelegate.m in Sources */,
DD52F251179CADC0005CD195 /* MASExampleBasicView.m in Sources */,
106A947E24F4C18B00F7A2D5 /* MASExampleLayoutGuideView.m in Sources */,
44C0E6AF1A9B9C55003C70CF /* MASExampleMarginView.m in Sources */,
DD653E4A1843E61500D1EC5A /* MASExampleLayoutGuideViewController.m in Sources */,
DDDF60CC181915E300BF7B8B /* MASExampleLabelView.m in Sources */,
Expand Down
17 changes: 17 additions & 0 deletions Examples/Masonry iOS Examples/MASExampleLayoutGuideView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// MASExampleLayoutGuideView.h
// Masonry iOS Examples
//
// Created by zhiqiang_ye on 2020/8/25.
// Copyright © 2020 Jonas Budelmann. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MASExampleLayoutGuideView : MAS_VIEW

@end

NS_ASSUME_NONNULL_END
78 changes: 78 additions & 0 deletions Examples/Masonry iOS Examples/MASExampleLayoutGuideView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// MASExampleLayoutGuideView.m
// Masonry iOS Examples
//
// Created by zhiqiang_ye on 2020/8/25.
// Copyright © 2020 Jonas Budelmann. All rights reserved.
//

#import "MASExampleLayoutGuideView.h"

@implementation MASExampleLayoutGuideView

#ifdef MAS_LAYOUT_GUIDE

- (id)init {
self = [super init];
if (!self) return nil;

[self example1];

return self;
}

- (void)example1{

MAS_VIEW *contentView = MAS_VIEW.new;
contentView.backgroundColor = self.randomColor;
contentView.layer.borderColor = self.randomColor.CGColor;
contentView.layer.borderWidth = 2;
[self addSubview:contentView];

MAS_VIEW *leftView = MAS_VIEW.new;
leftView.backgroundColor = self.randomColor;
leftView.layer.borderColor = self.randomColor.CGColor;
leftView.layer.borderWidth = 2;
[contentView addSubview:leftView];

MAS_VIEW *rightView = MAS_VIEW.new;
rightView.backgroundColor = self.randomColor;
rightView.layer.borderColor = self.randomColor.CGColor;
rightView.layer.borderWidth = 2;
[contentView addSubview:rightView];

[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.equalToSuperview();
make.height.mas_equalTo(self.mas_height).multipliedBy(0.3);
}];

MAS_LAYOUT_GUIDE *layout = [MAS_LAYOUT_GUIDE mas_allocWithOwningView:contentView];
[layout mas_makeConstraints:^(MASConstraintMaker * _Nonnull make) {
make.center.equalToSuperview();
make.size.mas_equalTo(CGSizeMake(60, 60));
}];

[leftView makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(layout);
make.right.mas_equalTo(layout.mas_left);
make.bottom.mas_equalTo(layout);
make.width.mas_equalTo(layout.mas_width);
}];

[rightView makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(layout);
make.left.mas_equalTo(layout.mas_right);
make.bottom.mas_equalTo(layout);
make.width.mas_equalTo(layout.mas_width);
}];
}

#endif

- (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
5 changes: 5 additions & 0 deletions Examples/Masonry iOS Examples/MASExampleListViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#import "MASExampleAspectFitView.h"
#import "MASExampleMarginView.h"
#import "MASExampleDistributeView.h"
#import "MASExampleLayoutGuideView.h"

static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";

Expand Down Expand Up @@ -83,6 +84,10 @@ - (id)init {
self.exampleControllers = [self.exampleControllers arrayByAddingObject:[[MASExampleSafeAreaLayoutGuideViewController alloc] init]];
}

#ifdef MAS_LAYOUT_GUIDE
self.exampleControllers = [self.exampleControllers arrayByAddingObject:[[MASExampleViewController alloc] initWithTitle:@"Layout Guide"
viewClass:MASExampleLayoutGuideView.class]];
#endif
return self;
}

Expand Down
19 changes: 19 additions & 0 deletions Masonry.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
106A947824F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 106A947624F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
106A947924F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 106A947724F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.m */; };
106A947A24F4BD8D00F7A2D5 /* LayoutGuide+MASAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 106A947624F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
106A947B24F4BD9500F7A2D5 /* LayoutGuide+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 106A947724F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.m */; };
106A949324F4FC8B00F7A2D5 /* MASItemConstraintAdditions+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 106A949124F4FC8B00F7A2D5 /* MASItemConstraintAdditions+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
1091037F24F7879A007EEB46 /* MASItemConstraintAdditions+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 106A949124F4FC8B00F7A2D5 /* MASItemConstraintAdditions+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
3AED05BD1AD59FD40053CC65 /* Masonry.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AED05BC1AD59FD40053CC65 /* Masonry.h */; settings = {ATTRIBUTES = (Public, ); }; };
3AED05F21AD5A0470053CC65 /* MASCompositeConstraint.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AED05DC1AD5A0470053CC65 /* MASCompositeConstraint.h */; settings = {ATTRIBUTES = (Public, ); }; };
3AED05F31AD5A0470053CC65 /* MASCompositeConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = 3AED05DD1AD5A0470053CC65 /* MASCompositeConstraint.m */; };
Expand Down Expand Up @@ -60,6 +66,9 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
106A947624F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "LayoutGuide+MASAdditions.h"; sourceTree = "<group>"; };
106A947724F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "LayoutGuide+MASAdditions.m"; sourceTree = "<group>"; };
106A949124F4FC8B00F7A2D5 /* MASItemConstraintAdditions+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "MASItemConstraintAdditions+Private.h"; sourceTree = "<group>"; };
3AED05B71AD59FD40053CC65 /* Masonry.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Masonry.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3AED05BB1AD59FD40053CC65 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3AED05BC1AD59FD40053CC65 /* Masonry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Masonry.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -130,8 +139,11 @@
children = (
3AED05BC1AD59FD40053CC65 /* Masonry.h */,
3AED05E51AD5A0470053CC65 /* MASUtilities.h */,
106A949124F4FC8B00F7A2D5 /* MASItemConstraintAdditions+Private.h */,
3AED05EF1AD5A0470053CC65 /* View+MASAdditions.h */,
3AED05F01AD5A0470053CC65 /* View+MASAdditions.m */,
106A947624F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.h */,
106A947724F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.m */,
3AED05F11AD5A0470053CC65 /* View+MASShorthandAdditions.h */,
4473548B1B39F772004DACCB /* ViewController+MASAdditions.h */,
4473548C1B39F772004DACCB /* ViewController+MASAdditions.m */,
Expand Down Expand Up @@ -186,7 +198,9 @@
3AED06001AD5A0470053CC65 /* NSArray+MASAdditions.h in Headers */,
3AED05F21AD5A0470053CC65 /* MASCompositeConstraint.h in Headers */,
3AED05F61AD5A0470053CC65 /* MASConstraint+Private.h in Headers */,
106A947824F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.h in Headers */,
3AED05F41AD5A0470053CC65 /* MASConstraint.h in Headers */,
106A949324F4FC8B00F7A2D5 /* MASItemConstraintAdditions+Private.h in Headers */,
3AED06031AD5A0470053CC65 /* NSLayoutConstraint+MASDebugAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -197,6 +211,7 @@
files = (
3AED06151AD5A1400053CC65 /* View+MASAdditions.h in Headers */,
3AED06161AD5A1400053CC65 /* View+MASShorthandAdditions.h in Headers */,
1091037F24F7879A007EEB46 /* MASItemConstraintAdditions+Private.h in Headers */,
3AED06171AD5A1400053CC65 /* MASViewAttribute.h in Headers */,
3AED06181AD5A1400053CC65 /* Masonry.h in Headers */,
3AED06191AD5A1400053CC65 /* MASLayoutConstraint.h in Headers */,
Expand All @@ -208,6 +223,7 @@
3AED06201AD5A1400053CC65 /* MASCompositeConstraint.h in Headers */,
447354931B3A18B9004DACCB /* ViewController+MASAdditions.h in Headers */,
3AED06221AD5A1400053CC65 /* MASConstraint.h in Headers */,
106A947A24F4BD8D00F7A2D5 /* LayoutGuide+MASAdditions.h in Headers */,
3AED061D1AD5A1400053CC65 /* MASConstraint+Private.h in Headers */,
3AED06211AD5A1400053CC65 /* NSLayoutConstraint+MASDebugAdditions.h in Headers */,
);
Expand Down Expand Up @@ -271,6 +287,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 3AED05AD1AD59FD40053CC65;
Expand Down Expand Up @@ -311,6 +328,7 @@
4473548E1B39F772004DACCB /* ViewController+MASAdditions.m in Sources */,
3AED05FA1AD5A0470053CC65 /* MASLayoutConstraint.m in Sources */,
3AED05F51AD5A0470053CC65 /* MASConstraint.m in Sources */,
106A947924F4AC8E00F7A2D5 /* LayoutGuide+MASAdditions.m in Sources */,
3AED05FF1AD5A0470053CC65 /* MASViewConstraint.m in Sources */,
3AED05F31AD5A0470053CC65 /* MASCompositeConstraint.m in Sources */,
3AED05F81AD5A0470053CC65 /* MASConstraintMaker.m in Sources */,
Expand All @@ -331,6 +349,7 @@
3AED060F1AD5A1400053CC65 /* MASCompositeConstraint.m in Sources */,
447354921B3A18B3004DACCB /* ViewController+MASAdditions.m in Sources */,
3AED06101AD5A1400053CC65 /* MASConstraintMaker.m in Sources */,
106A947B24F4BD9500F7A2D5 /* LayoutGuide+MASAdditions.m in Sources */,
3AED06111AD5A1400053CC65 /* NSLayoutConstraint+MASDebugAdditions.m in Sources */,
3AED06121AD5A1400053CC65 /* View+MASAdditions.m in Sources */,
);
Expand Down
96 changes: 96 additions & 0 deletions Masonry/LayoutGuide+MASAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// UILayoutGuide+MASAdditions.h
// Masonry iOS
//
// Created by zhiqiang_ye on 2020/8/25.
// Copyright © 2020 Jonas Budelmann. All rights reserved.
//

#import "MASUtilities.h"
#ifdef MAS_LAYOUT_GUIDE
#import "MASConstraintMaker.h"
#import "MASViewAttribute.h"

NS_ASSUME_NONNULL_BEGIN
/**
* Provides constraint maker block
* and convience methods for creating MASViewAttribute which are view + NSLayoutAttribute pairs
*/

@interface MAS_LAYOUT_GUIDE (MASAdditions)

/**
* following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute
*/
@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
@property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr);

@property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline;

@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;

/**
* a key to associate with this layout guide
*/
@property (nonatomic, strong) id mas_key;

/**
* Convenience initializer.
*/
+ (id)mas_allocWithOwningView:(MAS_VIEW *)owningView;

/**
* Creates a MASConstraintMaker with the callee layout guide.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing
*
* @param block scope within which you can build up the constraints which you wish to apply to the layout guide.
*
* @return Array of created MASConstraints
*/
- (NSArray *)mas_makeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

/**
* Creates a MASConstraintMaker with the callee layout guide.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing.
* If an existing constraint exists then it will be updated instead.
*
* @param block scope within which you can build up the constraints which you wish to apply to the layout guide.
*
* @return Array of created/updated MASConstraints
*/
- (NSArray *)mas_updateConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

/**
* Creates a MASConstraintMaker with the callee layout guide.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing.
* All constraints previously installed for the view will be removed.
*
* @param block scope within which you can build up the constraints which you wish to apply to the layout guide.
*
* @return Array of created/updated MASConstraints
*/
- (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;


@end

NS_ASSUME_NONNULL_END
#endif
Loading