├── MasonryDemo ├── Masonry │ ├── Info.plist │ ├── MASCompositeConstraint.h │ ├── MASCompositeConstraint.m │ ├── MASConstraint+Private.h │ ├── MASConstraint.h │ ├── MASConstraint.m │ ├── MASConstraintMaker.h │ ├── MASConstraintMaker.m │ ├── MASLayoutConstraint.h │ ├── MASLayoutConstraint.m │ ├── MASUtilities.h │ ├── MASViewAttribute.h │ ├── MASViewAttribute.m │ ├── MASViewConstraint.h │ ├── MASViewConstraint.m │ ├── Masonry.h │ ├── NSArray+MASAdditions.h │ ├── NSArray+MASAdditions.m │ ├── NSArray+MASShorthandAdditions.h │ ├── NSLayoutConstraint+MASDebugAdditions.h │ ├── NSLayoutConstraint+MASDebugAdditions.m │ ├── View+MASAdditions.h │ ├── View+MASAdditions.m │ ├── View+MASShorthandAdditions.h │ ├── ViewController+MASAdditions.h │ └── ViewController+MASAdditions.m ├── MasonryDemo.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ ├── xcshareddata │ │ │ └── MasonryDemo.xcscmblueprint │ │ └── xcuserdata │ │ │ └── lizelu.xcuserdatad │ │ │ └── UserInterfaceState.xcuserstate │ └── xcuserdata │ │ └── lizelu.xcuserdatad │ │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ │ └── xcschemes │ │ ├── MasonryDemo.xcscheme │ │ └── xcschememanagement.plist └── MasonryDemo │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── AspectFitWithRatioView.h │ ├── AspectFitWithRatioView.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ ├── Contents.json │ │ ├── icon_120-1.png │ │ ├── icon_120.png │ │ ├── icon_180.png │ │ ├── icon_58.png │ │ ├── icon_80.png │ │ └── icon_87.png │ ├── BaicView.h │ ├── BaicView.m │ ├── Base.lproj │ └── LaunchScreen.storyboard │ ├── BasicAnimatedView.h │ ├── BasicAnimatedView.m │ ├── DistributeView.h │ ├── DistributeView.m │ ├── Info.plist │ ├── MasonryTableViewController.h │ ├── MasonryTableViewController.m │ ├── PrefixHeader.pch │ ├── RemakeConstraintView.h │ ├── RemakeConstraintView.m │ ├── SubViewController.h │ ├── SubViewController.m │ ├── UpdateArrayViews.h │ ├── UpdateArrayViews.m │ ├── UpdateConstraintView.h │ ├── UpdateConstraintView.m │ ├── UseConstantsView.h │ ├── UseConstantsView.m │ ├── UseEdgesInsetView.h │ ├── UseEdgesInsetView.m │ ├── UserMarginView.h │ ├── UserMarginView.m │ ├── heart.png │ └── main.m ├── README.md └── 类图.png /MasonryDemo/Masonry/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASCompositeConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | /** 13 | * A group of MASConstraint objects 14 | */ 15 | @interface MASCompositeConstraint : MASConstraint 16 | 17 | /** 18 | * Creates a composite with a predefined array of children 19 | * 20 | * @param children child MASConstraints 21 | * 22 | * @return a composite constraint 23 | */ 24 | - (id)initWithChildren:(NSArray *)children; 25 | 26 | @end 27 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASCompositeConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASCompositeConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASCompositeConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | 12 | @interface MASCompositeConstraint () 13 | 14 | @property (nonatomic, strong) id mas_key; 15 | @property (nonatomic, strong) NSMutableArray *childConstraints; 16 | 17 | @end 18 | 19 | @implementation MASCompositeConstraint 20 | 21 | - (id)initWithChildren:(NSArray *)children { 22 | self = [super init]; 23 | if (!self) return nil; 24 | 25 | _childConstraints = [children mutableCopy]; 26 | for (MASConstraint *constraint in _childConstraints) { 27 | constraint.delegate = self; 28 | } 29 | 30 | return self; 31 | } 32 | 33 | #pragma mark - MASConstraintDelegate 34 | 35 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 36 | NSUInteger index = [self.childConstraints indexOfObject:constraint]; 37 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 38 | [self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint]; 39 | } 40 | 41 | - (MASConstraint *)constraint:(MASConstraint __unused *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 42 | id strongDelegate = self.delegate; 43 | MASConstraint *newConstraint = [strongDelegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 44 | newConstraint.delegate = self; 45 | [self.childConstraints addObject:newConstraint]; 46 | return newConstraint; 47 | } 48 | 49 | #pragma mark - NSLayoutConstraint multiplier proxies 50 | 51 | - (MASConstraint * (^)(CGFloat))multipliedBy { 52 | return ^id(CGFloat multiplier) { 53 | for (MASConstraint *constraint in self.childConstraints) { 54 | constraint.multipliedBy(multiplier); 55 | } 56 | return self; 57 | }; 58 | } 59 | 60 | - (MASConstraint * (^)(CGFloat))dividedBy { 61 | return ^id(CGFloat divider) { 62 | for (MASConstraint *constraint in self.childConstraints) { 63 | constraint.dividedBy(divider); 64 | } 65 | return self; 66 | }; 67 | } 68 | 69 | #pragma mark - MASLayoutPriority proxy 70 | 71 | - (MASConstraint * (^)(MASLayoutPriority))priority { 72 | return ^id(MASLayoutPriority priority) { 73 | for (MASConstraint *constraint in self.childConstraints) { 74 | constraint.priority(priority); 75 | } 76 | return self; 77 | }; 78 | } 79 | 80 | #pragma mark - NSLayoutRelation proxy 81 | 82 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 83 | return ^id(id attr, NSLayoutRelation relation) { 84 | for (MASConstraint *constraint in self.childConstraints.copy) { 85 | constraint.equalToWithRelation(attr, relation); 86 | } 87 | return self; 88 | }; 89 | } 90 | 91 | #pragma mark - attribute chaining 92 | 93 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 94 | [self constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 95 | return self; 96 | } 97 | 98 | #pragma mark - Animator proxy 99 | 100 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 101 | 102 | - (MASConstraint *)animator { 103 | for (MASConstraint *constraint in self.childConstraints) { 104 | [constraint animator]; 105 | } 106 | return self; 107 | } 108 | 109 | #endif 110 | 111 | #pragma mark - debug helpers 112 | 113 | - (MASConstraint * (^)(id))key { 114 | return ^id(id key) { 115 | self.mas_key = key; 116 | int i = 0; 117 | for (MASConstraint *constraint in self.childConstraints) { 118 | constraint.key([NSString stringWithFormat:@"%@[%d]", key, i++]); 119 | } 120 | return self; 121 | }; 122 | } 123 | 124 | #pragma mark - NSLayoutConstraint constant setters 125 | 126 | - (void)setInsets:(MASEdgeInsets)insets { 127 | for (MASConstraint *constraint in self.childConstraints) { 128 | constraint.insets = insets; 129 | } 130 | } 131 | 132 | - (void)setOffset:(CGFloat)offset { 133 | for (MASConstraint *constraint in self.childConstraints) { 134 | constraint.offset = offset; 135 | } 136 | } 137 | 138 | - (void)setSizeOffset:(CGSize)sizeOffset { 139 | for (MASConstraint *constraint in self.childConstraints) { 140 | constraint.sizeOffset = sizeOffset; 141 | } 142 | } 143 | 144 | - (void)setCenterOffset:(CGPoint)centerOffset { 145 | for (MASConstraint *constraint in self.childConstraints) { 146 | constraint.centerOffset = centerOffset; 147 | } 148 | } 149 | 150 | #pragma mark - MASConstraint 151 | 152 | - (void)activate { 153 | for (MASConstraint *constraint in self.childConstraints) { 154 | [constraint activate]; 155 | } 156 | } 157 | 158 | - (void)deactivate { 159 | for (MASConstraint *constraint in self.childConstraints) { 160 | [constraint deactivate]; 161 | } 162 | } 163 | 164 | - (void)install { 165 | for (MASConstraint *constraint in self.childConstraints) { 166 | constraint.updateExisting = self.updateExisting; 167 | [constraint install]; 168 | } 169 | } 170 | 171 | - (void)uninstall { 172 | for (MASConstraint *constraint in self.childConstraints) { 173 | [constraint uninstall]; 174 | } 175 | } 176 | 177 | @end 178 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASConstraint+Private.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint+Private.h 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 29/04/14. 6 | // Copyright (c) 2014 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | 11 | @protocol MASConstraintDelegate; 12 | 13 | 14 | @interface MASConstraint () 15 | 16 | /** 17 | * Whether or not to check for an existing constraint instead of adding constraint 18 | */ 19 | @property (nonatomic, assign) BOOL updateExisting; 20 | 21 | /** 22 | * Usually MASConstraintMaker but could be a parent MASConstraint 23 | */ 24 | @property (nonatomic, weak) id delegate; 25 | 26 | /** 27 | * Based on a provided value type, is equal to calling: 28 | * NSNumber - setOffset: 29 | * NSValue with CGPoint - setPointOffset: 30 | * NSValue with CGSize - setSizeOffset: 31 | * NSValue with MASEdgeInsets - setInsets: 32 | */ 33 | - (void)setLayoutConstantWithValue:(NSValue *)value; 34 | 35 | @end 36 | 37 | 38 | @interface MASConstraint (Abstract) 39 | 40 | /** 41 | * Sets the constraint relation to given NSLayoutRelation 42 | * returns a block which accepts one of the following: 43 | * MASViewAttribute, UIView, NSValue, NSArray 44 | * see readme for more details. 45 | */ 46 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation; 47 | 48 | /** 49 | * Override to set a custom chaining behaviour 50 | */ 51 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 52 | 53 | @end 54 | 55 | 56 | @protocol MASConstraintDelegate 57 | 58 | /** 59 | * Notifies the delegate when the constraint needs to be replaced with another constraint. For example 60 | * A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks 61 | */ 62 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint; 63 | 64 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * Enables Constraints to be created with chainable syntax 13 | * Constraint can represent single NSLayoutConstraint (MASViewConstraint) 14 | * or a group of NSLayoutConstraints (MASComposisteConstraint) 15 | */ 16 | @interface MASConstraint : NSObject 17 | 18 | // Chaining Support 19 | 20 | /** 21 | * Modifies the NSLayoutConstraint constant, 22 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 23 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 24 | */ 25 | - (MASConstraint * (^)(MASEdgeInsets insets))insets; 26 | 27 | /** 28 | * Modifies the NSLayoutConstraint constant, 29 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 30 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 31 | */ 32 | - (MASConstraint * (^)(CGSize offset))sizeOffset; 33 | 34 | /** 35 | * Modifies the NSLayoutConstraint constant, 36 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 37 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 38 | */ 39 | - (MASConstraint * (^)(CGPoint offset))centerOffset; 40 | 41 | /** 42 | * Modifies the NSLayoutConstraint constant 43 | */ 44 | - (MASConstraint * (^)(CGFloat offset))offset; 45 | 46 | /** 47 | * Modifies the NSLayoutConstraint constant based on a value type 48 | */ 49 | - (MASConstraint * (^)(NSValue *value))valueOffset; 50 | 51 | /** 52 | * Sets the NSLayoutConstraint multiplier property 53 | */ 54 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy; 55 | 56 | /** 57 | * Sets the NSLayoutConstraint multiplier to 1.0/dividedBy 58 | */ 59 | - (MASConstraint * (^)(CGFloat divider))dividedBy; 60 | 61 | /** 62 | * Sets the NSLayoutConstraint priority to a float or MASLayoutPriority 63 | */ 64 | - (MASConstraint * (^)(MASLayoutPriority priority))priority; 65 | 66 | /** 67 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityLow 68 | */ 69 | - (MASConstraint * (^)())priorityLow; 70 | 71 | /** 72 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium 73 | */ 74 | - (MASConstraint * (^)())priorityMedium; 75 | 76 | /** 77 | * Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh 78 | */ 79 | - (MASConstraint * (^)())priorityHigh; 80 | 81 | /** 82 | * Sets the constraint relation to NSLayoutRelationEqual 83 | * returns a block which accepts one of the following: 84 | * MASViewAttribute, UIView, NSValue, NSArray 85 | * see readme for more details. 86 | */ 87 | - (MASConstraint * (^)(id attr))equalTo; 88 | 89 | /** 90 | * Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual 91 | * returns a block which accepts one of the following: 92 | * MASViewAttribute, UIView, NSValue, NSArray 93 | * see readme for more details. 94 | */ 95 | - (MASConstraint * (^)(id attr))greaterThanOrEqualTo; 96 | 97 | /** 98 | * Sets the constraint relation to NSLayoutRelationLessThanOrEqual 99 | * returns a block which accepts one of the following: 100 | * MASViewAttribute, UIView, NSValue, NSArray 101 | * see readme for more details. 102 | */ 103 | - (MASConstraint * (^)(id attr))lessThanOrEqualTo; 104 | 105 | /** 106 | * Optional semantic property which has no effect but improves the readability of constraint 107 | */ 108 | - (MASConstraint *)with; 109 | 110 | /** 111 | * Optional semantic property which has no effect but improves the readability of constraint 112 | */ 113 | - (MASConstraint *)and; 114 | 115 | /** 116 | * Creates a new MASCompositeConstraint with the called attribute and reciever 117 | */ 118 | - (MASConstraint *)left; 119 | - (MASConstraint *)top; 120 | - (MASConstraint *)right; 121 | - (MASConstraint *)bottom; 122 | - (MASConstraint *)leading; 123 | - (MASConstraint *)trailing; 124 | - (MASConstraint *)width; 125 | - (MASConstraint *)height; 126 | - (MASConstraint *)centerX; 127 | - (MASConstraint *)centerY; 128 | - (MASConstraint *)baseline; 129 | 130 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 131 | 132 | - (MASConstraint *)firstBaseline; 133 | - (MASConstraint *)lastBaseline; 134 | 135 | #endif 136 | 137 | #if TARGET_OS_IPHONE || TARGET_OS_TV 138 | 139 | - (MASConstraint *)leftMargin; 140 | - (MASConstraint *)rightMargin; 141 | - (MASConstraint *)topMargin; 142 | - (MASConstraint *)bottomMargin; 143 | - (MASConstraint *)leadingMargin; 144 | - (MASConstraint *)trailingMargin; 145 | - (MASConstraint *)centerXWithinMargins; 146 | - (MASConstraint *)centerYWithinMargins; 147 | 148 | #endif 149 | 150 | 151 | /** 152 | * Sets the constraint debug name 153 | */ 154 | - (MASConstraint * (^)(id key))key; 155 | 156 | // NSLayoutConstraint constant Setters 157 | // for use outside of mas_updateConstraints/mas_makeConstraints blocks 158 | 159 | /** 160 | * Modifies the NSLayoutConstraint constant, 161 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 162 | * NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight 163 | */ 164 | - (void)setInsets:(MASEdgeInsets)insets; 165 | 166 | /** 167 | * Modifies the NSLayoutConstraint constant, 168 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 169 | * NSLayoutAttributeWidth, NSLayoutAttributeHeight 170 | */ 171 | - (void)setSizeOffset:(CGSize)sizeOffset; 172 | 173 | /** 174 | * Modifies the NSLayoutConstraint constant, 175 | * only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 176 | * NSLayoutAttributeCenterX, NSLayoutAttributeCenterY 177 | */ 178 | - (void)setCenterOffset:(CGPoint)centerOffset; 179 | 180 | /** 181 | * Modifies the NSLayoutConstraint constant 182 | */ 183 | - (void)setOffset:(CGFloat)offset; 184 | 185 | 186 | // NSLayoutConstraint Installation support 187 | 188 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 189 | /** 190 | * Whether or not to go through the animator proxy when modifying the constraint 191 | */ 192 | @property (nonatomic, copy, readonly) MASConstraint *animator; 193 | #endif 194 | 195 | /** 196 | * Activates an NSLayoutConstraint if it's supported by an OS. 197 | * Invokes install otherwise. 198 | */ 199 | - (void)activate; 200 | 201 | /** 202 | * Deactivates previously installed/activated NSLayoutConstraint. 203 | */ 204 | - (void)deactivate; 205 | 206 | /** 207 | * Creates a NSLayoutConstraint and adds it to the appropriate view. 208 | */ 209 | - (void)install; 210 | 211 | /** 212 | * Removes previously installed NSLayoutConstraint 213 | */ 214 | - (void)uninstall; 215 | 216 | @end 217 | 218 | 219 | /** 220 | * Convenience auto-boxing macros for MASConstraint methods. 221 | * 222 | * Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax. 223 | * A potential drawback of this is that the unprefixed macros will appear in global scope. 224 | */ 225 | #define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__))) 226 | #define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 227 | #define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__))) 228 | 229 | #define mas_offset(...) valueOffset(MASBoxValue((__VA_ARGS__))) 230 | 231 | 232 | #ifdef MAS_SHORTHAND_GLOBALS 233 | 234 | #define equalTo(...) mas_equalTo(__VA_ARGS__) 235 | #define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__) 236 | #define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__) 237 | 238 | #define offset(...) mas_offset(__VA_ARGS__) 239 | 240 | #endif 241 | 242 | 243 | @interface MASConstraint (AutoboxingSupport) 244 | 245 | /** 246 | * Aliases to corresponding relation methods (for shorthand macros) 247 | * Also needed to aid autocompletion 248 | */ 249 | - (MASConstraint * (^)(id attr))mas_equalTo; 250 | - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo; 251 | - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo; 252 | 253 | /** 254 | * A dummy method to aid autocompletion 255 | */ 256 | - (MASConstraint * (^)(id offset))mas_offset; 257 | 258 | @end 259 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.m 3 | // Masonry 4 | // 5 | // Created by Nick Tymchenko on 1/20/14. 6 | // 7 | 8 | #import "MASConstraint.h" 9 | #import "MASConstraint+Private.h" 10 | 11 | #define MASMethodNotImplemented() \ 12 | @throw [NSException exceptionWithName:NSInternalInconsistencyException \ 13 | reason:[NSString stringWithFormat:@"You must override %@ in a subclass.", NSStringFromSelector(_cmd)] \ 14 | userInfo:nil] 15 | 16 | @implementation MASConstraint 17 | 18 | #pragma mark - Init 19 | 20 | - (id)init { 21 | NSAssert(![self isMemberOfClass:[MASConstraint class]], @"MASConstraint is an abstract class, you should not instantiate it directly."); 22 | return [super init]; 23 | } 24 | 25 | #pragma mark - NSLayoutRelation proxies 26 | 27 | - (MASConstraint * (^)(id))equalTo { 28 | return ^id(id attribute) { 29 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 30 | }; 31 | } 32 | 33 | - (MASConstraint * (^)(id))mas_equalTo { 34 | return ^id(id attribute) { 35 | return self.equalToWithRelation(attribute, NSLayoutRelationEqual); 36 | }; 37 | } 38 | 39 | - (MASConstraint * (^)(id))greaterThanOrEqualTo { 40 | return ^id(id attribute) { 41 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 42 | }; 43 | } 44 | 45 | - (MASConstraint * (^)(id))mas_greaterThanOrEqualTo { 46 | return ^id(id attribute) { 47 | return self.equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual); 48 | }; 49 | } 50 | 51 | - (MASConstraint * (^)(id))lessThanOrEqualTo { 52 | return ^id(id attribute) { 53 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 54 | }; 55 | } 56 | 57 | - (MASConstraint * (^)(id))mas_lessThanOrEqualTo { 58 | return ^id(id attribute) { 59 | return self.equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual); 60 | }; 61 | } 62 | 63 | #pragma mark - MASLayoutPriority proxies 64 | 65 | - (MASConstraint * (^)())priorityLow { 66 | return ^id{ 67 | self.priority(MASLayoutPriorityDefaultLow); 68 | return self; 69 | }; 70 | } 71 | 72 | - (MASConstraint * (^)())priorityMedium { 73 | return ^id{ 74 | self.priority(MASLayoutPriorityDefaultMedium); 75 | return self; 76 | }; 77 | } 78 | 79 | - (MASConstraint * (^)())priorityHigh { 80 | return ^id{ 81 | self.priority(MASLayoutPriorityDefaultHigh); 82 | return self; 83 | }; 84 | } 85 | 86 | #pragma mark - NSLayoutConstraint constant proxies 87 | 88 | - (MASConstraint * (^)(MASEdgeInsets))insets { 89 | return ^id(MASEdgeInsets insets){ 90 | self.insets = insets; 91 | return self; 92 | }; 93 | } 94 | 95 | - (MASConstraint * (^)(CGSize))sizeOffset { 96 | return ^id(CGSize offset) { 97 | self.sizeOffset = offset; 98 | return self; 99 | }; 100 | } 101 | 102 | - (MASConstraint * (^)(CGPoint))centerOffset { 103 | return ^id(CGPoint offset) { 104 | self.centerOffset = offset; 105 | return self; 106 | }; 107 | } 108 | 109 | - (MASConstraint * (^)(CGFloat))offset { 110 | return ^id(CGFloat offset){ 111 | self.offset = offset; 112 | return self; 113 | }; 114 | } 115 | 116 | - (MASConstraint * (^)(NSValue *value))valueOffset { 117 | return ^id(NSValue *offset) { 118 | NSAssert([offset isKindOfClass:NSValue.class], @"expected an NSValue offset, got: %@", offset); 119 | [self setLayoutConstantWithValue:offset]; 120 | return self; 121 | }; 122 | } 123 | 124 | - (MASConstraint * (^)(id offset))mas_offset { 125 | // Will never be called due to macro 126 | return nil; 127 | } 128 | 129 | #pragma mark - NSLayoutConstraint constant setter 130 | 131 | - (void)setLayoutConstantWithValue:(NSValue *)value { 132 | if ([value isKindOfClass:NSNumber.class]) { 133 | self.offset = [(NSNumber *)value doubleValue]; 134 | } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) { 135 | CGPoint point; 136 | [value getValue:&point]; 137 | self.centerOffset = point; 138 | } else if (strcmp(value.objCType, @encode(CGSize)) == 0) { 139 | CGSize size; 140 | [value getValue:&size]; 141 | self.sizeOffset = size; 142 | } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) { 143 | MASEdgeInsets insets; 144 | [value getValue:&insets]; 145 | self.insets = insets; 146 | } else { 147 | NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value); 148 | } 149 | } 150 | 151 | #pragma mark - Semantic properties 152 | 153 | - (MASConstraint *)with { 154 | return self; 155 | } 156 | 157 | - (MASConstraint *)and { 158 | return self; 159 | } 160 | 161 | #pragma mark - Chaining 162 | 163 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute __unused)layoutAttribute { 164 | MASMethodNotImplemented(); 165 | } 166 | 167 | - (MASConstraint *)left { 168 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 169 | } 170 | 171 | - (MASConstraint *)top { 172 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 173 | } 174 | 175 | - (MASConstraint *)right { 176 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 177 | } 178 | 179 | - (MASConstraint *)bottom { 180 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 181 | } 182 | 183 | - (MASConstraint *)leading { 184 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 185 | } 186 | 187 | - (MASConstraint *)trailing { 188 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 189 | } 190 | 191 | - (MASConstraint *)width { 192 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 193 | } 194 | 195 | - (MASConstraint *)height { 196 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 197 | } 198 | 199 | - (MASConstraint *)centerX { 200 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 201 | } 202 | 203 | - (MASConstraint *)centerY { 204 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 205 | } 206 | 207 | - (MASConstraint *)baseline { 208 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 209 | } 210 | 211 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 212 | 213 | - (MASConstraint *)firstBaseline { 214 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 215 | } 216 | - (MASConstraint *)lastBaseline { 217 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 218 | } 219 | 220 | #endif 221 | 222 | #if TARGET_OS_IPHONE || TARGET_OS_TV 223 | 224 | - (MASConstraint *)leftMargin { 225 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 226 | } 227 | 228 | - (MASConstraint *)rightMargin { 229 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 230 | } 231 | 232 | - (MASConstraint *)topMargin { 233 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 234 | } 235 | 236 | - (MASConstraint *)bottomMargin { 237 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 238 | } 239 | 240 | - (MASConstraint *)leadingMargin { 241 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 242 | } 243 | 244 | - (MASConstraint *)trailingMargin { 245 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 246 | } 247 | 248 | - (MASConstraint *)centerXWithinMargins { 249 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 250 | } 251 | 252 | - (MASConstraint *)centerYWithinMargins { 253 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 254 | } 255 | 256 | #endif 257 | 258 | #pragma mark - Abstract 259 | 260 | - (MASConstraint * (^)(CGFloat multiplier))multipliedBy { MASMethodNotImplemented(); } 261 | 262 | - (MASConstraint * (^)(CGFloat divider))dividedBy { MASMethodNotImplemented(); } 263 | 264 | - (MASConstraint * (^)(MASLayoutPriority priority))priority { MASMethodNotImplemented(); } 265 | 266 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { MASMethodNotImplemented(); } 267 | 268 | - (MASConstraint * (^)(id key))key { MASMethodNotImplemented(); } 269 | 270 | - (void)setInsets:(MASEdgeInsets __unused)insets { MASMethodNotImplemented(); } 271 | 272 | - (void)setSizeOffset:(CGSize __unused)sizeOffset { MASMethodNotImplemented(); } 273 | 274 | - (void)setCenterOffset:(CGPoint __unused)centerOffset { MASMethodNotImplemented(); } 275 | 276 | - (void)setOffset:(CGFloat __unused)offset { MASMethodNotImplemented(); } 277 | 278 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 279 | 280 | - (MASConstraint *)animator { MASMethodNotImplemented(); } 281 | 282 | #endif 283 | 284 | - (void)activate { MASMethodNotImplemented(); } 285 | 286 | - (void)deactivate { MASMethodNotImplemented(); } 287 | 288 | - (void)install { MASMethodNotImplemented(); } 289 | 290 | - (void)uninstall { MASMethodNotImplemented(); } 291 | 292 | @end 293 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASConstraintMaker.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintBuilder.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraint.h" 10 | #import "MASUtilities.h" 11 | 12 | typedef NS_OPTIONS(NSInteger, MASAttribute) { 13 | MASAttributeLeft = 1 << NSLayoutAttributeLeft, 14 | MASAttributeRight = 1 << NSLayoutAttributeRight, 15 | MASAttributeTop = 1 << NSLayoutAttributeTop, 16 | MASAttributeBottom = 1 << NSLayoutAttributeBottom, 17 | MASAttributeLeading = 1 << NSLayoutAttributeLeading, 18 | MASAttributeTrailing = 1 << NSLayoutAttributeTrailing, 19 | MASAttributeWidth = 1 << NSLayoutAttributeWidth, 20 | MASAttributeHeight = 1 << NSLayoutAttributeHeight, 21 | MASAttributeCenterX = 1 << NSLayoutAttributeCenterX, 22 | MASAttributeCenterY = 1 << NSLayoutAttributeCenterY, 23 | MASAttributeBaseline = 1 << NSLayoutAttributeBaseline, 24 | 25 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 26 | 27 | MASAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline, 28 | MASAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline, 29 | 30 | #endif 31 | 32 | #if TARGET_OS_IPHONE || TARGET_OS_TV 33 | 34 | MASAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin, 35 | MASAttributeRightMargin = 1 << NSLayoutAttributeRightMargin, 36 | MASAttributeTopMargin = 1 << NSLayoutAttributeTopMargin, 37 | MASAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin, 38 | MASAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin, 39 | MASAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin, 40 | MASAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins, 41 | MASAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins, 42 | 43 | #endif 44 | 45 | }; 46 | 47 | /** 48 | * Provides factory methods for creating MASConstraints. 49 | * Constraints are collected until they are ready to be installed 50 | * 51 | */ 52 | @interface MASConstraintMaker : NSObject 53 | 54 | /** 55 | * The following properties return a new MASViewConstraint 56 | * with the first item set to the makers associated view and the appropriate MASViewAttribute 57 | */ 58 | @property (nonatomic, strong, readonly) MASConstraint *left; 59 | @property (nonatomic, strong, readonly) MASConstraint *top; 60 | @property (nonatomic, strong, readonly) MASConstraint *right; 61 | @property (nonatomic, strong, readonly) MASConstraint *bottom; 62 | @property (nonatomic, strong, readonly) MASConstraint *leading; 63 | @property (nonatomic, strong, readonly) MASConstraint *trailing; 64 | @property (nonatomic, strong, readonly) MASConstraint *width; 65 | @property (nonatomic, strong, readonly) MASConstraint *height; 66 | @property (nonatomic, strong, readonly) MASConstraint *centerX; 67 | @property (nonatomic, strong, readonly) MASConstraint *centerY; 68 | @property (nonatomic, strong, readonly) MASConstraint *baseline; 69 | 70 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 71 | 72 | @property (nonatomic, strong, readonly) MASConstraint *firstBaseline; 73 | @property (nonatomic, strong, readonly) MASConstraint *lastBaseline; 74 | 75 | #endif 76 | 77 | #if TARGET_OS_IPHONE || TARGET_OS_TV 78 | 79 | @property (nonatomic, strong, readonly) MASConstraint *leftMargin; 80 | @property (nonatomic, strong, readonly) MASConstraint *rightMargin; 81 | @property (nonatomic, strong, readonly) MASConstraint *topMargin; 82 | @property (nonatomic, strong, readonly) MASConstraint *bottomMargin; 83 | @property (nonatomic, strong, readonly) MASConstraint *leadingMargin; 84 | @property (nonatomic, strong, readonly) MASConstraint *trailingMargin; 85 | @property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins; 86 | @property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins; 87 | 88 | #endif 89 | 90 | /** 91 | * Returns a block which creates a new MASCompositeConstraint with the first item set 92 | * to the makers associated view and children corresponding to the set bits in the 93 | * MASAttribute parameter. Combine multiple attributes via binary-or. 94 | */ 95 | @property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs); 96 | 97 | /** 98 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeEdges 99 | * which generates the appropriate MASViewConstraint children (top, left, bottom, right) 100 | * with the first item set to the makers associated view 101 | */ 102 | @property (nonatomic, strong, readonly) MASConstraint *edges; 103 | 104 | /** 105 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeSize 106 | * which generates the appropriate MASViewConstraint children (width, height) 107 | * with the first item set to the makers associated view 108 | */ 109 | @property (nonatomic, strong, readonly) MASConstraint *size; 110 | 111 | /** 112 | * Creates a MASCompositeConstraint with type MASCompositeConstraintTypeCenter 113 | * which generates the appropriate MASViewConstraint children (centerX, centerY) 114 | * with the first item set to the makers associated view 115 | */ 116 | @property (nonatomic, strong, readonly) MASConstraint *center; 117 | 118 | /** 119 | * Whether or not to check for an existing constraint instead of adding constraint 120 | */ 121 | @property (nonatomic, assign) BOOL updateExisting; 122 | 123 | /** 124 | * Whether or not to remove existing constraints prior to installing 125 | */ 126 | @property (nonatomic, assign) BOOL removeExisting; 127 | 128 | /** 129 | * initialises the maker with a default view 130 | * 131 | * @param view any MASConstrait are created with this view as the first item 132 | * 133 | * @return a new MASConstraintMaker 134 | */ 135 | - (id)initWithView:(MAS_VIEW *)view; 136 | 137 | /** 138 | * Calls install method on any MASConstraints which have been created by this maker 139 | * 140 | * @return an array of all the installed MASConstraints 141 | */ 142 | - (NSArray *)install; 143 | 144 | - (MASConstraint * (^)(dispatch_block_t))group; 145 | 146 | @end 147 | 148 | //回调配置成员属性的Block类型 149 | typedef void(^MASConstraintMakerConfigBlock)(MASConstraintMaker *make); 150 | 151 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASConstraintMaker.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraintBuilder.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASConstraintMaker.h" 10 | #import "MASViewConstraint.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASConstraint+Private.h" 13 | #import "MASViewAttribute.h" 14 | #import "View+MASAdditions.h" 15 | 16 | @interface MASConstraintMaker () 17 | 18 | @property (nonatomic, weak) MAS_VIEW *view; //要添加约束的视图 19 | @property (nonatomic, strong) NSMutableArray *constraints; //存储添加到View上的约束 20 | 21 | @end 22 | 23 | @implementation MASConstraintMaker 24 | 25 | - (id)initWithView:(MAS_VIEW *)view { 26 | self = [super init]; 27 | if (!self) return nil; 28 | 29 | self.view = view; 30 | self.constraints = NSMutableArray.new; 31 | 32 | return self; 33 | } 34 | 35 | //往View上Install约束 36 | - (NSArray *)install { 37 | 38 | //如果是mas_remakeConstraint, 要先将该视图上的约束先uninstall 39 | if (self.removeExisting) { 40 | //获取当前视图上添加的所有约束(NSArray) 41 | NSArray *installedConstraints = [MASViewConstraint installedConstraintsForView:self.view]; 42 | 43 | //移除掉所有约束 44 | for (MASConstraint *constraint in installedConstraints) { 45 | [constraint uninstall]; 46 | } 47 | } 48 | 49 | //添加约束 50 | //self.constraints中存储的是通过Block中配置的参数 51 | NSArray *constraints = self.constraints.copy; 52 | for (MASConstraint *constraint in constraints) { 53 | constraint.updateExisting = self.updateExisting; //updateExisting默认是NO 54 | [constraint install]; //install每个约束 55 | } 56 | [self.constraints removeAllObjects]; 57 | return constraints; 58 | } 59 | 60 | #pragma mark - MASConstraintDelegate 61 | 62 | //将constraints数组中的某些元素进行替换 63 | - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint { 64 | NSUInteger index = [self.constraints indexOfObject:constraint]; 65 | NSAssert(index != NSNotFound, @"Could not find constraint %@", constraint); 66 | [self.constraints replaceObjectAtIndex:index withObject:replacementConstraint]; 67 | } 68 | 69 | - (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 70 | //创建ViewAttribute 71 | MASViewAttribute *viewAttribute = [[MASViewAttribute alloc] initWithView:self.view layoutAttribute:layoutAttribute]; 72 | 73 | //根据ViewAttribute创建ViewConstraint 74 | MASViewConstraint *newConstraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:viewAttribute]; 75 | 76 | //constraint不为nil是,就和newConstraint合并组合成MASCompositeConstraint对象 77 | if ([constraint isKindOfClass:MASViewConstraint.class]) { 78 | //replace with composite constraint 79 | NSArray *children = @[constraint, newConstraint]; 80 | 81 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 82 | compositeConstraint.delegate = self; 83 | [self constraint:constraint shouldBeReplacedWithConstraint:compositeConstraint]; 84 | return compositeConstraint; 85 | } 86 | 87 | if (!constraint) { 88 | newConstraint.delegate = self; //设置代理-MASConstraintDelegate,为了MASViewConstraint也可以调用该方法 89 | [self.constraints addObject:newConstraint]; //添加进数组 90 | } 91 | return newConstraint; 92 | } 93 | 94 | - (MASConstraint *)addConstraintWithAttributes:(MASAttribute)attrs { 95 | __unused MASAttribute anyAttribute = (MASAttributeLeft | MASAttributeRight | MASAttributeTop | MASAttributeBottom | MASAttributeLeading 96 | | MASAttributeTrailing | MASAttributeWidth | MASAttributeHeight | MASAttributeCenterX 97 | | MASAttributeCenterY | MASAttributeBaseline 98 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 99 | | MASAttributeFirstBaseline | MASAttributeLastBaseline 100 | #endif 101 | #if TARGET_OS_IPHONE || TARGET_OS_TV 102 | | MASAttributeLeftMargin | MASAttributeRightMargin | MASAttributeTopMargin | MASAttributeBottomMargin 103 | | MASAttributeLeadingMargin | MASAttributeTrailingMargin | MASAttributeCenterXWithinMargins 104 | | MASAttributeCenterYWithinMargins 105 | #endif 106 | ); 107 | 108 | NSAssert((attrs & anyAttribute) != 0, @"You didn't pass any attribute to make.attributes(...)"); 109 | 110 | NSMutableArray *attributes = [NSMutableArray array]; 111 | 112 | if (attrs & MASAttributeLeft) [attributes addObject:self.view.mas_left]; 113 | if (attrs & MASAttributeRight) [attributes addObject:self.view.mas_right]; 114 | if (attrs & MASAttributeTop) [attributes addObject:self.view.mas_top]; 115 | if (attrs & MASAttributeBottom) [attributes addObject:self.view.mas_bottom]; 116 | if (attrs & MASAttributeLeading) [attributes addObject:self.view.mas_leading]; 117 | if (attrs & MASAttributeTrailing) [attributes addObject:self.view.mas_trailing]; 118 | if (attrs & MASAttributeWidth) [attributes addObject:self.view.mas_width]; 119 | if (attrs & MASAttributeHeight) [attributes addObject:self.view.mas_height]; 120 | if (attrs & MASAttributeCenterX) [attributes addObject:self.view.mas_centerX]; 121 | if (attrs & MASAttributeCenterY) [attributes addObject:self.view.mas_centerY]; 122 | if (attrs & MASAttributeBaseline) [attributes addObject:self.view.mas_baseline]; 123 | 124 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 125 | 126 | if (attrs & MASAttributeFirstBaseline) [attributes addObject:self.view.mas_firstBaseline]; 127 | if (attrs & MASAttributeLastBaseline) [attributes addObject:self.view.mas_lastBaseline]; 128 | 129 | #endif 130 | 131 | #if TARGET_OS_IPHONE || TARGET_OS_TV 132 | 133 | if (attrs & MASAttributeLeftMargin) [attributes addObject:self.view.mas_leftMargin]; 134 | if (attrs & MASAttributeRightMargin) [attributes addObject:self.view.mas_rightMargin]; 135 | if (attrs & MASAttributeTopMargin) [attributes addObject:self.view.mas_topMargin]; 136 | if (attrs & MASAttributeBottomMargin) [attributes addObject:self.view.mas_bottomMargin]; 137 | if (attrs & MASAttributeLeadingMargin) [attributes addObject:self.view.mas_leadingMargin]; 138 | if (attrs & MASAttributeTrailingMargin) [attributes addObject:self.view.mas_trailingMargin]; 139 | if (attrs & MASAttributeCenterXWithinMargins) [attributes addObject:self.view.mas_centerXWithinMargins]; 140 | if (attrs & MASAttributeCenterYWithinMargins) [attributes addObject:self.view.mas_centerYWithinMargins]; 141 | 142 | #endif 143 | 144 | NSMutableArray *children = [NSMutableArray arrayWithCapacity:attributes.count]; 145 | 146 | for (MASViewAttribute *a in attributes) { 147 | [children addObject:[[MASViewConstraint alloc] initWithFirstViewAttribute:a]]; 148 | } 149 | 150 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 151 | constraint.delegate = self; 152 | [self.constraints addObject:constraint]; 153 | return constraint; 154 | } 155 | 156 | #pragma mark - standard Attributes 157 | 158 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 159 | return [self constraint:nil addConstraintWithLayoutAttribute:layoutAttribute]; 160 | } 161 | 162 | - (MASConstraint *)left { 163 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeft]; 164 | } 165 | 166 | - (MASConstraint *)top { 167 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTop]; 168 | } 169 | 170 | - (MASConstraint *)right { 171 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRight]; 172 | } 173 | 174 | - (MASConstraint *)bottom { 175 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottom]; 176 | } 177 | 178 | - (MASConstraint *)leading { 179 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeading]; 180 | } 181 | 182 | - (MASConstraint *)trailing { 183 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailing]; 184 | } 185 | 186 | - (MASConstraint *)width { 187 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeWidth]; 188 | } 189 | 190 | - (MASConstraint *)height { 191 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeHeight]; 192 | } 193 | 194 | - (MASConstraint *)centerX { 195 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterX]; 196 | } 197 | 198 | - (MASConstraint *)centerY { 199 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterY]; 200 | } 201 | 202 | - (MASConstraint *)baseline { 203 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBaseline]; 204 | } 205 | 206 | - (MASConstraint *(^)(MASAttribute))attributes { 207 | return ^(MASAttribute attrs){ 208 | return [self addConstraintWithAttributes:attrs]; 209 | }; 210 | } 211 | 212 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 213 | 214 | - (MASConstraint *)firstBaseline { 215 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeFirstBaseline]; 216 | } 217 | 218 | - (MASConstraint *)lastBaseline { 219 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLastBaseline]; 220 | } 221 | 222 | #endif 223 | 224 | 225 | #if TARGET_OS_IPHONE || TARGET_OS_TV 226 | 227 | - (MASConstraint *)leftMargin { 228 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeftMargin]; 229 | } 230 | 231 | - (MASConstraint *)rightMargin { 232 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeRightMargin]; 233 | } 234 | 235 | - (MASConstraint *)topMargin { 236 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTopMargin]; 237 | } 238 | 239 | - (MASConstraint *)bottomMargin { 240 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeBottomMargin]; 241 | } 242 | 243 | - (MASConstraint *)leadingMargin { 244 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeLeadingMargin]; 245 | } 246 | 247 | - (MASConstraint *)trailingMargin { 248 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeTrailingMargin]; 249 | } 250 | 251 | - (MASConstraint *)centerXWithinMargins { 252 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 253 | } 254 | 255 | - (MASConstraint *)centerYWithinMargins { 256 | return [self addConstraintWithLayoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 257 | } 258 | 259 | #endif 260 | 261 | 262 | #pragma mark - composite Attributes 263 | 264 | - (MASConstraint *)edges { 265 | return [self addConstraintWithAttributes:MASAttributeTop | MASAttributeLeft | MASAttributeRight | MASAttributeBottom]; 266 | } 267 | 268 | - (MASConstraint *)size { 269 | return [self addConstraintWithAttributes:MASAttributeWidth | MASAttributeHeight]; 270 | } 271 | 272 | - (MASConstraint *)center { 273 | return [self addConstraintWithAttributes:MASAttributeCenterX | MASAttributeCenterY]; 274 | } 275 | 276 | #pragma mark - grouping 277 | 278 | - (MASConstraint *(^)(dispatch_block_t group))group { 279 | return ^id(dispatch_block_t group) { 280 | NSInteger previousCount = self.constraints.count; 281 | group(); 282 | 283 | NSArray *children = [self.constraints subarrayWithRange:NSMakeRange(previousCount, self.constraints.count - previousCount)]; 284 | MASCompositeConstraint *constraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 285 | constraint.delegate = self; 286 | return constraint; 287 | }; 288 | } 289 | 290 | @end 291 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASLayoutConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * When you are debugging or printing the constraints attached to a view this subclass 13 | * makes it easier to identify which constraints have been created via Masonry 14 | */ 15 | @interface MASLayoutConstraint : NSLayoutConstraint 16 | 17 | /** 18 | * a key to associate with this constraint 19 | */ 20 | @property (nonatomic, strong) id mas_key; 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASLayoutConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASLayoutConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASLayoutConstraint.h" 10 | 11 | @implementation MASLayoutConstraint 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASUtilities.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASUtilities.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 19/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | 13 | #if TARGET_OS_IPHONE || TARGET_OS_TV 14 | 15 | #import 16 | #define MAS_VIEW UIView 17 | #define MAS_VIEW_CONTROLLER UIViewController 18 | #define MASEdgeInsets UIEdgeInsets 19 | 20 | typedef UILayoutPriority MASLayoutPriority; 21 | static const MASLayoutPriority MASLayoutPriorityRequired = UILayoutPriorityRequired; 22 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = UILayoutPriorityDefaultHigh; 23 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 500; 24 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = UILayoutPriorityDefaultLow; 25 | static const MASLayoutPriority MASLayoutPriorityFittingSizeLevel = UILayoutPriorityFittingSizeLevel; 26 | 27 | #elif TARGET_OS_MAC 28 | 29 | #import 30 | #define MAS_VIEW NSView 31 | #define MASEdgeInsets NSEdgeInsets 32 | 33 | typedef NSLayoutPriority MASLayoutPriority; 34 | static const MASLayoutPriority MASLayoutPriorityRequired = NSLayoutPriorityRequired; 35 | static const MASLayoutPriority MASLayoutPriorityDefaultHigh = NSLayoutPriorityDefaultHigh; 36 | static const MASLayoutPriority MASLayoutPriorityDragThatCanResizeWindow = NSLayoutPriorityDragThatCanResizeWindow; 37 | static const MASLayoutPriority MASLayoutPriorityDefaultMedium = 501; 38 | static const MASLayoutPriority MASLayoutPriorityWindowSizeStayPut = NSLayoutPriorityWindowSizeStayPut; 39 | static const MASLayoutPriority MASLayoutPriorityDragThatCannotResizeWindow = NSLayoutPriorityDragThatCannotResizeWindow; 40 | static const MASLayoutPriority MASLayoutPriorityDefaultLow = NSLayoutPriorityDefaultLow; 41 | static const MASLayoutPriority MASLayoutPriorityFittingSizeCompression = NSLayoutPriorityFittingSizeCompression; 42 | 43 | #endif 44 | 45 | /** 46 | * Allows you to attach keys to objects matching the variable names passed. 47 | * 48 | * view1.mas_key = @"view1", view2.mas_key = @"view2"; 49 | * 50 | * is equivalent to: 51 | * 52 | * MASAttachKeys(view1, view2); 53 | */ 54 | #define MASAttachKeys(...) \ 55 | { \ 56 | NSDictionary *keyPairs = NSDictionaryOfVariableBindings(__VA_ARGS__); \ 57 | for (id key in keyPairs.allKeys) { \ 58 | id obj = keyPairs[key]; \ 59 | NSAssert([obj respondsToSelector:@selector(setMas_key:)], \ 60 | @"Cannot attach mas_key to %@", obj); \ 61 | [obj setMas_key:key]; \ 62 | } \ 63 | } 64 | 65 | /** 66 | * Used to create object hashes 67 | * Based on http://www.mikeash.com/pyblog/friday-qa-2010-06-18-implementing-equality-and-hashing.html 68 | */ 69 | #define MAS_NSUINT_BIT (CHAR_BIT * sizeof(NSUInteger)) 70 | #define MAS_NSUINTROTATE(val, howmuch) ((((NSUInteger)val) << howmuch) | (((NSUInteger)val) >> (MAS_NSUINT_BIT - howmuch))) 71 | 72 | /** 73 | * Given a scalar or struct value, wraps it in NSValue 74 | * Based on EXPObjectify: https://github.com/specta/expecta 75 | */ 76 | static inline id _MASBoxValue(const char *type, ...) { 77 | va_list v; 78 | va_start(v, type); 79 | id obj = nil; 80 | if (strcmp(type, @encode(id)) == 0) { 81 | id actual = va_arg(v, id); 82 | obj = actual; 83 | } else if (strcmp(type, @encode(CGPoint)) == 0) { 84 | CGPoint actual = (CGPoint)va_arg(v, CGPoint); 85 | obj = [NSValue value:&actual withObjCType:type]; 86 | } else if (strcmp(type, @encode(CGSize)) == 0) { 87 | CGSize actual = (CGSize)va_arg(v, CGSize); 88 | obj = [NSValue value:&actual withObjCType:type]; 89 | } else if (strcmp(type, @encode(MASEdgeInsets)) == 0) { 90 | MASEdgeInsets actual = (MASEdgeInsets)va_arg(v, MASEdgeInsets); 91 | obj = [NSValue value:&actual withObjCType:type]; 92 | } else if (strcmp(type, @encode(double)) == 0) { 93 | double actual = (double)va_arg(v, double); 94 | obj = [NSNumber numberWithDouble:actual]; 95 | } else if (strcmp(type, @encode(float)) == 0) { 96 | float actual = (float)va_arg(v, double); 97 | obj = [NSNumber numberWithFloat:actual]; 98 | } else if (strcmp(type, @encode(int)) == 0) { 99 | int actual = (int)va_arg(v, int); 100 | obj = [NSNumber numberWithInt:actual]; 101 | } else if (strcmp(type, @encode(long)) == 0) { 102 | long actual = (long)va_arg(v, long); 103 | obj = [NSNumber numberWithLong:actual]; 104 | } else if (strcmp(type, @encode(long long)) == 0) { 105 | long long actual = (long long)va_arg(v, long long); 106 | obj = [NSNumber numberWithLongLong:actual]; 107 | } else if (strcmp(type, @encode(short)) == 0) { 108 | short actual = (short)va_arg(v, int); 109 | obj = [NSNumber numberWithShort:actual]; 110 | } else if (strcmp(type, @encode(char)) == 0) { 111 | char actual = (char)va_arg(v, int); 112 | obj = [NSNumber numberWithChar:actual]; 113 | } else if (strcmp(type, @encode(bool)) == 0) { 114 | bool actual = (bool)va_arg(v, int); 115 | obj = [NSNumber numberWithBool:actual]; 116 | } else if (strcmp(type, @encode(unsigned char)) == 0) { 117 | unsigned char actual = (unsigned char)va_arg(v, unsigned int); 118 | obj = [NSNumber numberWithUnsignedChar:actual]; 119 | } else if (strcmp(type, @encode(unsigned int)) == 0) { 120 | unsigned int actual = (unsigned int)va_arg(v, unsigned int); 121 | obj = [NSNumber numberWithUnsignedInt:actual]; 122 | } else if (strcmp(type, @encode(unsigned long)) == 0) { 123 | unsigned long actual = (unsigned long)va_arg(v, unsigned long); 124 | obj = [NSNumber numberWithUnsignedLong:actual]; 125 | } else if (strcmp(type, @encode(unsigned long long)) == 0) { 126 | unsigned long long actual = (unsigned long long)va_arg(v, unsigned long long); 127 | obj = [NSNumber numberWithUnsignedLongLong:actual]; 128 | } else if (strcmp(type, @encode(unsigned short)) == 0) { 129 | unsigned short actual = (unsigned short)va_arg(v, unsigned int); 130 | obj = [NSNumber numberWithUnsignedShort:actual]; 131 | } 132 | va_end(v); 133 | return obj; 134 | } 135 | 136 | #define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value)) 137 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASViewAttribute.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASAttribute.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * An immutable tuple which stores the view and the related NSLayoutAttribute. 13 | * Describes part of either the left or right hand side of a constraint equation 14 | */ 15 | @interface MASViewAttribute : NSObject 16 | 17 | /** 18 | * The view which the reciever relates to. Can be nil if item is not a view. 19 | * 20 | * 所要约束的View 21 | */ 22 | @property (nonatomic, weak, readonly) MAS_VIEW *view; 23 | 24 | /** 25 | * The item which the reciever relates to. 26 | * 27 | * 可约束的对象(UIView就是其本身,而UIViewController就是 28 | * self.topLayoutGuide 和 self.bottomLayoutGuide 29 | * item是为UIViewController准备的。 30 | */ 31 | @property (nonatomic, weak, readonly) id item; 32 | 33 | /** 34 | * The attribute which the reciever relates to 35 | * 36 | * View所接触的布局属性(NSLayoutAttributeLeft,NSLayoutAttributeTop等) 37 | */ 38 | @property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute; 39 | 40 | /** 41 | * Convenience initializer. 42 | * 43 | * UIView一般调用下方的方法,因为UIView的 view == item 44 | * 也就是当前View就是接受布局关系的对象 45 | */ 46 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute; 47 | 48 | /** 49 | * The designated initializer. 50 | * 51 | * 下方的方法一般是UIViewController调用的方法,在UIViewController中接受约束的是self.topLayoutGuide 52 | * 和self.BottomLayoutGuide, 此时View != item 53 | */ 54 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute; 55 | 56 | /** 57 | * Determine whether the layoutAttribute is a size attribute 58 | * 59 | * @return YES if layoutAttribute is equal to NSLayoutAttributeWidth or NSLayoutAttributeHeight 60 | * 61 | * 判断是否为NSLayoutAttributeWidth和NSLayoutAttributeHeight类型的约束 62 | * 如果是的话就不需要其他参考的View, 将上述两种类型的约束直接添加到本View上即可 63 | */ 64 | - (BOOL)isSizeAttribute; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASViewAttribute.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASAttribute.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 21/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | 11 | @implementation MASViewAttribute 12 | 13 | /** 14 | * 调用该方法时 view == item 15 | * 16 | * @param view 17 | * @param layoutAttribute 18 | * 19 | * @return 20 | */ 21 | - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute { 22 | self = [self initWithView:view item:view layoutAttribute:layoutAttribute]; 23 | return self; 24 | } 25 | 26 | - (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute { 27 | self = [super init]; 28 | if (!self) return nil; 29 | 30 | _view = view; 31 | _item = item; 32 | _layoutAttribute = layoutAttribute; 33 | 34 | return self; 35 | } 36 | 37 | - (BOOL)isSizeAttribute { 38 | return self.layoutAttribute == NSLayoutAttributeWidth 39 | || self.layoutAttribute == NSLayoutAttributeHeight; 40 | } 41 | 42 | // 43 | // 44 | /** 45 | * 重写的NSObject的isEqual方法,用来比较自定义的ViewAttribute对象是否相等 46 | * view和layoutAttributte成员属性相同就说明两个ViewAttribute对象相等 47 | * 48 | * @param viewAttribute 要比较的viewAttribute 49 | * 50 | * @return 返回的结果 51 | */ 52 | - (BOOL)isEqual:(MASViewAttribute *)viewAttribute { 53 | if ([viewAttribute isKindOfClass:self.class]) { 54 | return self.view == viewAttribute.view 55 | && self.layoutAttribute == viewAttribute.layoutAttribute; 56 | } 57 | return [super isEqual:viewAttribute]; 58 | } 59 | 60 | /** 61 | * 为当前MASViewAttribute的对象生成hash值 62 | * 63 | * @return 哈希值 64 | */ 65 | - (NSUInteger)hash { 66 | return MAS_NSUINTROTATE([self.view hash], MAS_NSUINT_BIT / 2) ^ self.layoutAttribute; 67 | } 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASViewConstraint.h: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewAttribute.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | #import "MASUtilities.h" 13 | 14 | /** 15 | * A single constraint. 16 | * Contains the attributes neccessary for creating a NSLayoutConstraint and adding it to the appropriate view 17 | */ 18 | @interface MASViewConstraint : MASConstraint 19 | 20 | /** 21 | * First item/view and first attribute of the NSLayoutConstraint 22 | */ 23 | @property (nonatomic, strong, readonly) MASViewAttribute *firstViewAttribute; //当前视图要添加的属性 24 | 25 | /** 26 | * Second item/view and second attribute of the NSLayoutConstraint 27 | */ 28 | @property (nonatomic, strong, readonly) MASViewAttribute *secondViewAttribute; //相对于其他视图的视图属性 29 | 30 | /** 31 | * initialises the MASViewConstraint with the first part of the equation 32 | * 33 | * @param firstViewAttribute view.mas_left, view.mas_width etc. 34 | * 35 | * @return a new view constraint 36 | */ 37 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute; 38 | 39 | /** 40 | * Returns all MASViewConstraints installed with this view as a first item. 41 | * 42 | * @param view A view to retrieve constraints for. 43 | * 44 | * @return An array of MASViewConstraints. 45 | */ 46 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view; 47 | 48 | @end 49 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/MASViewConstraint.m: -------------------------------------------------------------------------------- 1 | // 2 | // MASConstraint.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASViewConstraint.h" 10 | #import "MASConstraint+Private.h" 11 | #import "MASCompositeConstraint.h" 12 | #import "MASLayoutConstraint.h" 13 | #import "View+MASAdditions.h" 14 | #import 15 | 16 | 17 | 18 | 19 | 20 | /** 21 | * UIView+MASConstraints, 私有类目,只有MASConstraints类用到该类目 22 | */ 23 | 24 | @interface MAS_VIEW (MASConstraints) 25 | 26 | @property (nonatomic, readonly) NSMutableSet *mas_installedConstraints; 27 | 28 | @end 29 | 30 | @implementation MAS_VIEW (MASConstraints) 31 | 32 | static char kInstalledConstraintsKey; //动态添加属性的key, 用来表示动态添加的属性 33 | /** 34 | * 通过运行时动态的添加或者获取约束集合(NSMutableSet constraints) 35 | * 36 | * @return NSMutableSet类型的约束 37 | */ 38 | - (NSMutableSet *)mas_installedConstraints { 39 | 40 | //通过kInstalledConstraintsKey获取动态添加的已安装的约束集合 41 | NSMutableSet *constraints = objc_getAssociatedObject(self, &kInstalledConstraintsKey); 42 | 43 | //如果constants == nil, 说明未动态绑定该成员变量,就进行动态绑定 44 | if (!constraints) { 45 | 46 | constraints = [NSMutableSet set]; 47 | 48 | //动态为View添加已安装的约束集合(constraints),并且为该成员指定唯一标示(kInstalledConstraintsKey) 49 | //OBJC_ASSOCIATION_RETAIN_NONATOMIC == (strong, nonatomic) 50 | objc_setAssociatedObject(self, &kInstalledConstraintsKey, constraints, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 51 | } 52 | 53 | return constraints; 54 | } 55 | 56 | @end 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | @interface MASViewConstraint () 68 | 69 | @property (nonatomic, strong, readwrite) MASViewAttribute *secondViewAttribute; //相对视图的约束 70 | @property (nonatomic, weak) MAS_VIEW *installedView; //布局约束所添加的视图 71 | @property (nonatomic, weak) MASLayoutConstraint *layoutConstraint; //约束对象: left, top, bottom等 72 | @property (nonatomic, assign) NSLayoutRelation layoutRelation; //约束关系:=,>=,<= 73 | @property (nonatomic, assign) MASLayoutPriority layoutPriority; //约束的优先级:Low, High等 74 | @property (nonatomic, assign) CGFloat layoutMultiplier; //倍数 75 | @property (nonatomic, assign) CGFloat layoutConstant; //约束的值:top = 10(布局常量) 76 | @property (nonatomic, assign) BOOL hasLayoutRelation; //标记是否有布局关系 77 | @property (nonatomic, strong) id mas_key; 78 | @property (nonatomic, assign) BOOL useAnimator; 79 | 80 | @end 81 | 82 | @implementation MASViewConstraint 83 | 84 | - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute { 85 | self = [super init]; 86 | if (!self) return nil; 87 | 88 | _firstViewAttribute = firstViewAttribute; 89 | self.layoutPriority = MASLayoutPriorityRequired; //约束等级默认为“必须的” 90 | self.layoutMultiplier = 1; //倍数默认为“1” 91 | 92 | return self; 93 | } 94 | 95 | #pragma mark - NSCoping 96 | /** 97 | * 实现拷贝协议,支持MASViewConstraint对象的拷贝 98 | * 99 | * @param zone 100 | * 101 | * @return 拷贝后的MASViewConstraint对象 102 | */ 103 | - (id)copyWithZone:(NSZone __unused *)zone { 104 | MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:self.firstViewAttribute]; 105 | constraint.layoutConstant = self.layoutConstant; 106 | constraint.layoutRelation = self.layoutRelation; 107 | constraint.layoutPriority = self.layoutPriority; 108 | constraint.layoutMultiplier = self.layoutMultiplier; 109 | constraint.delegate = self.delegate; 110 | return constraint; 111 | } 112 | 113 | #pragma mark - Public 114 | /** 115 | * 获取传入View的所有被Install的约束 116 | * 117 | * @param view 约束所安装的视图 118 | * 119 | * @return NSArray 120 | */ 121 | + (NSArray *)installedConstraintsForView:(MAS_VIEW *)view { 122 | return [view.mas_installedConstraints allObjects]; 123 | } 124 | 125 | #pragma mark - Private 126 | 127 | /** 128 | * 布局常量(layoutConstant)的Setter方法 129 | * 130 | * @param layoutConstant 布局常量的值 131 | */ 132 | - (void)setLayoutConstant:(CGFloat)layoutConstant { 133 | _layoutConstant = layoutConstant; 134 | 135 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 136 | if (self.useAnimator) { 137 | [self.layoutConstraint.animator setConstant:layoutConstant]; 138 | } else { 139 | self.layoutConstraint.constant = layoutConstant; 140 | } 141 | #else 142 | //为成员变量-布局约束layoutConstraint设置constant值,该布局约束的值,类似于top.constant = 10; 143 | self.layoutConstraint.constant = layoutConstant; 144 | #endif 145 | } 146 | 147 | 148 | 149 | /** 150 | * 设置布局关系 151 | * 152 | * @param layoutRelation 153 | * 布局关系值 154 | * NSLayoutRelationEqual 155 | * NSLayoutRelationLessThanOrEqual 156 | * NSLayoutRelationGreaterThanOrEqual 157 | * 158 | */ 159 | - (void)setLayoutRelation:(NSLayoutRelation)layoutRelation { 160 | _layoutRelation = layoutRelation; //设置约束关系 161 | self.hasLayoutRelation = YES; //标记已设置约束关系 162 | } 163 | 164 | /** 165 | * 判断layoutConstraint是否可以使用“isActive”方法 166 | * 167 | * @return <#return value description#> 168 | */ 169 | - (BOOL)supportsActiveProperty { 170 | return [self.layoutConstraint respondsToSelector:@selector(isActive)]; 171 | } 172 | 173 | /** 174 | * 判断布局是否被激活 175 | * 176 | * @return 激活状态 177 | */ 178 | - (BOOL)isActive { 179 | BOOL active = YES; 180 | if ([self supportsActiveProperty]) { 181 | active = [self.layoutConstraint isActive]; 182 | } 183 | 184 | return active; 185 | } 186 | 187 | 188 | /** 189 | * 判断布局是否被安装 190 | * 191 | * @return 布尔值 192 | */ 193 | - (BOOL)hasBeenInstalled { 194 | return (self.layoutConstraint != nil) && [self isActive]; 195 | } 196 | 197 | 198 | 199 | /** 200 | * 成员属性secondViewAttribute的setter方法 201 | * 202 | * @param secondViewAttribute 203 | */ 204 | - (void)setSecondViewAttribute:(id)secondViewAttribute { 205 | if ([secondViewAttribute isKindOfClass:NSValue.class]) { //直接传过来的是Value 206 | [self setLayoutConstantWithValue:secondViewAttribute]; 207 | } else if ([secondViewAttribute isKindOfClass:MAS_VIEW.class]) { //传过来的是一个UIView 208 | _secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondViewAttribute layoutAttribute:self.firstViewAttribute.layoutAttribute]; 209 | } else if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) {//传过来的是一个约束,如mas_top 210 | _secondViewAttribute = secondViewAttribute; 211 | } else { 212 | NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute); 213 | } 214 | } 215 | 216 | #pragma mark - NSLayoutConstraint multiplier proxies 217 | 218 | - (MASConstraint * (^)(CGFloat))multipliedBy { 219 | return ^id(CGFloat multiplier) { 220 | NSAssert(!self.hasBeenInstalled, 221 | @"Cannot modify constraint multiplier after it has been installed"); 222 | 223 | self.layoutMultiplier = multiplier; 224 | return self; 225 | }; 226 | } 227 | 228 | 229 | - (MASConstraint * (^)(CGFloat))dividedBy { 230 | return ^id(CGFloat divider) { 231 | NSAssert(!self.hasBeenInstalled, 232 | @"Cannot modify constraint multiplier after it has been installed"); 233 | 234 | self.layoutMultiplier = 1.0/divider; 235 | return self; 236 | }; 237 | } 238 | 239 | #pragma mark - MASLayoutPriority proxy 240 | 241 | - (MASConstraint * (^)(MASLayoutPriority))priority { 242 | return ^id(MASLayoutPriority priority) { 243 | NSAssert(!self.hasBeenInstalled, 244 | @"Cannot modify constraint priority after it has been installed"); 245 | 246 | self.layoutPriority = priority; 247 | return self; 248 | }; 249 | } 250 | 251 | #pragma mark - NSLayoutRelation proxy 252 | 253 | - (MASConstraint * (^)(id, NSLayoutRelation))equalToWithRelation { 254 | return ^id(id attribute, NSLayoutRelation relation) { 255 | 256 | if ([attribute isKindOfClass:NSArray.class]) { //参数为数组的情况 257 | 258 | NSAssert(!self.hasLayoutRelation, @"Redefinition of constraint relation"); 259 | 260 | //将不变数组转换成可变数组 261 | NSMutableArray *children = NSMutableArray.new; 262 | for (id attr in attribute) { 263 | MASViewConstraint *viewConstraint = [self copy]; 264 | viewConstraint.secondViewAttribute = attr; //将数组中的元素转换成MASViewAttribute对象 265 | [children addObject:viewConstraint]; 266 | } 267 | 268 | //将数NSArray换成MASCompositeConstraint 269 | MASCompositeConstraint *compositeConstraint = [[MASCompositeConstraint alloc] initWithChildren:children]; 270 | compositeConstraint.delegate = self.delegate; 271 | [self.delegate constraint:self shouldBeReplacedWithConstraint:compositeConstraint]; 272 | return compositeConstraint; 273 | } else { 274 | NSAssert(!self.hasLayoutRelation || self.layoutRelation == relation && [attribute isKindOfClass:NSValue.class], @"Redefinition of constraint relation"); 275 | self.layoutRelation = relation; 276 | self.secondViewAttribute = attribute; 277 | return self; 278 | } 279 | }; 280 | } 281 | 282 | #pragma mark - Semantic properties 283 | 284 | - (MASConstraint *)with { 285 | return self; 286 | } 287 | 288 | - (MASConstraint *)and { 289 | return self; 290 | } 291 | 292 | #pragma mark - attribute chaining 293 | 294 | - (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute { 295 | NSAssert(!self.hasLayoutRelation, @"Attributes should be chained before defining the constraint relation"); 296 | return [self.delegate constraint:self addConstraintWithLayoutAttribute:layoutAttribute]; 297 | } 298 | 299 | #pragma mark - Animator proxy 300 | 301 | #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV) 302 | 303 | - (MASConstraint *)animator { 304 | self.useAnimator = YES; 305 | return self; 306 | } 307 | 308 | #endif 309 | 310 | #pragma mark - debug helpers 311 | 312 | - (MASConstraint * (^)(id))key { 313 | return ^id(id key) { 314 | self.mas_key = key; 315 | return self; 316 | }; 317 | } 318 | 319 | #pragma mark - NSLayoutConstraint constant setters 320 | 321 | - (void)setInsets:(MASEdgeInsets)insets { 322 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 323 | switch (layoutAttribute) { 324 | case NSLayoutAttributeLeft: 325 | case NSLayoutAttributeLeading: 326 | self.layoutConstant = insets.left; 327 | break; 328 | case NSLayoutAttributeTop: 329 | self.layoutConstant = insets.top; 330 | break; 331 | case NSLayoutAttributeBottom: 332 | self.layoutConstant = -insets.bottom; 333 | break; 334 | case NSLayoutAttributeRight: 335 | case NSLayoutAttributeTrailing: 336 | self.layoutConstant = -insets.right; 337 | break; 338 | default: 339 | break; 340 | } 341 | } 342 | 343 | - (void)setOffset:(CGFloat)offset { 344 | self.layoutConstant = offset; 345 | } 346 | 347 | - (void)setSizeOffset:(CGSize)sizeOffset { 348 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 349 | switch (layoutAttribute) { 350 | case NSLayoutAttributeWidth: 351 | self.layoutConstant = sizeOffset.width; 352 | break; 353 | case NSLayoutAttributeHeight: 354 | self.layoutConstant = sizeOffset.height; 355 | break; 356 | default: 357 | break; 358 | } 359 | } 360 | 361 | - (void)setCenterOffset:(CGPoint)centerOffset { 362 | NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute; 363 | switch (layoutAttribute) { 364 | case NSLayoutAttributeCenterX: 365 | self.layoutConstant = centerOffset.x; 366 | break; 367 | case NSLayoutAttributeCenterY: 368 | self.layoutConstant = centerOffset.y; 369 | break; 370 | default: 371 | break; 372 | } 373 | } 374 | 375 | #pragma mark - MASConstraint 376 | 377 | - (void)activate { 378 | [self install]; 379 | } 380 | 381 | - (void)deactivate { 382 | [self uninstall]; 383 | } 384 | 385 | - (void)install { 386 | //如果已经添加过约束,就return 387 | if (self.hasBeenInstalled) { 388 | return; 389 | } 390 | 391 | //如果layoutConstraint可以使用“isActive”方法,并且self.layoutConstraint不为nil 392 | if ([self supportsActiveProperty] && self.layoutConstraint) { 393 | 394 | //激活约束 395 | self.layoutConstraint.active = YES; 396 | 397 | //将已激活的约束添加到当前View的已安装约束的数组中 398 | [self.firstViewAttribute.view.mas_installedConstraints addObject:self]; 399 | return; 400 | } 401 | 402 | MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item; 403 | NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute; 404 | 405 | MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item; 406 | NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute; 407 | 408 | // alignment attributes must have a secondViewAttribute 409 | // therefore we assume that is refering to superview 410 | // eg make.left.equalTo(@10) 411 | if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) { 412 | secondLayoutItem = self.firstViewAttribute.view.superview; 413 | secondLayoutAttribute = firstLayoutAttribute; 414 | } 415 | 416 | 417 | #pragma -- Mark 使用NSLayoutConstraint添加约束 418 | 419 | //创建约束 420 | MASLayoutConstraint *layoutConstraint 421 | = [MASLayoutConstraint constraintWithItem:firstLayoutItem 422 | attribute:firstLayoutAttribute 423 | relatedBy:self.layoutRelation 424 | toItem:secondLayoutItem 425 | attribute:secondLayoutAttribute 426 | multiplier:self.layoutMultiplier 427 | constant:self.layoutConstant]; 428 | 429 | layoutConstraint.priority = self.layoutPriority; 430 | layoutConstraint.mas_key = self.mas_key; 431 | 432 | //寻找约束添加的View 433 | if (self.secondViewAttribute.view) { 434 | //寻找两个视图的公共父视图 435 | MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view 436 | mas_closestCommonSuperview:self.secondViewAttribute.view]; 437 | self.installedView = closestCommonSuperview; 438 | } else if (self.firstViewAttribute.isSizeAttribute) { 439 | self.installedView = self.firstViewAttribute.view; 440 | } else { 441 | self.installedView = self.firstViewAttribute.view.superview; 442 | } 443 | 444 | 445 | MASLayoutConstraint *existingConstraint = nil; 446 | if (self.updateExisting) { 447 | existingConstraint = [self layoutConstraintSimilarTo:layoutConstraint]; 448 | } 449 | 450 | if (existingConstraint) { 451 | // just update the constant 452 | //更新约束 453 | existingConstraint.constant = layoutConstraint.constant; 454 | self.layoutConstraint = existingConstraint; 455 | } else { 456 | //添加约束 457 | [self.installedView addConstraint:layoutConstraint]; 458 | self.layoutConstraint = layoutConstraint; 459 | [firstLayoutItem.mas_installedConstraints addObject:self]; //约束所在的View增加被添加的约束 460 | } 461 | } 462 | 463 | - (MASLayoutConstraint *)layoutConstraintSimilarTo:(MASLayoutConstraint *)layoutConstraint { 464 | // check if any constraints are the same apart from the only mutable property constant 465 | 466 | // go through constraints in reverse as we do not want to match auto-resizing or interface builder constraints 467 | // and they are likely to be added first. 468 | for (NSLayoutConstraint *existingConstraint in self.installedView.constraints.reverseObjectEnumerator) { 469 | if (![existingConstraint isKindOfClass:MASLayoutConstraint.class]) continue; 470 | if (existingConstraint.firstItem != layoutConstraint.firstItem) continue; 471 | if (existingConstraint.secondItem != layoutConstraint.secondItem) continue; 472 | if (existingConstraint.firstAttribute != layoutConstraint.firstAttribute) continue; 473 | if (existingConstraint.secondAttribute != layoutConstraint.secondAttribute) continue; 474 | if (existingConstraint.relation != layoutConstraint.relation) continue; 475 | if (existingConstraint.multiplier != layoutConstraint.multiplier) continue; 476 | if (existingConstraint.priority != layoutConstraint.priority) continue; 477 | 478 | return (id)existingConstraint; 479 | } 480 | return nil; 481 | } 482 | 483 | - (void)uninstall { 484 | if ([self supportsActiveProperty]) { 485 | self.layoutConstraint.active = NO; 486 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 487 | return; 488 | } 489 | 490 | [self.installedView removeConstraint:self.layoutConstraint]; 491 | self.layoutConstraint = nil; 492 | self.installedView = nil; 493 | 494 | [self.firstViewAttribute.view.mas_installedConstraints removeObject:self]; 495 | } 496 | 497 | @end 498 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/Masonry.h: -------------------------------------------------------------------------------- 1 | // 2 | // Masonry.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for Masonry. 12 | FOUNDATION_EXPORT double MasonryVersionNumber; 13 | 14 | //! Project version string for Masonry. 15 | FOUNDATION_EXPORT const unsigned char MasonryVersionString[]; 16 | 17 | #import "MASUtilities.h" 18 | #import "View+MASAdditions.h" 19 | #import "View+MASShorthandAdditions.h" 20 | #import "ViewController+MASAdditions.h" 21 | #import "NSArray+MASAdditions.h" 22 | #import "NSArray+MASShorthandAdditions.h" 23 | #import "MASConstraint.h" 24 | #import "MASCompositeConstraint.h" 25 | #import "MASViewAttribute.h" 26 | #import "MASViewConstraint.h" 27 | #import "MASConstraintMaker.h" 28 | #import "MASLayoutConstraint.h" 29 | #import "NSLayoutConstraint+MASDebugAdditions.h" 30 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/NSArray+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.h 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | typedef NS_ENUM(NSUInteger, MASAxisType) { 14 | MASAxisTypeHorizontal, 15 | MASAxisTypeVertical 16 | }; 17 | 18 | @interface NSArray (MASAdditions) 19 | 20 | /** 21 | * Creates a MASConstraintMaker with each view in the callee. 22 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing on each view 23 | * 24 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 25 | * 26 | * @return Array of created MASConstraints 27 | */ 28 | - (NSArray *)mas_makeConstraints:(MASConstraintMakerConfigBlock)block; 29 | 30 | /** 31 | * Creates a MASConstraintMaker with each view in the callee. 32 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 33 | * If an existing constraint exists then it will be updated instead. 34 | * 35 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 36 | * 37 | * @return Array of created/updated MASConstraints 38 | */ 39 | - (NSArray *)mas_updateConstraints:(MASConstraintMakerConfigBlock)block; 40 | 41 | /** 42 | * Creates a MASConstraintMaker with each view in the callee. 43 | * Any constraints defined are added to each view or the appropriate superview once the block has finished executing on each view. 44 | * All constraints previously installed for the views will be removed. 45 | * 46 | * @param block scope within which you can build up the constraints which you wish to apply to each view. 47 | * 48 | * @return Array of created/updated MASConstraints 49 | */ 50 | - (NSArray *)mas_remakeConstraints:(MASConstraintMakerConfigBlock)block; 51 | 52 | /** 53 | * distribute with fixed spacing 54 | * 55 | * @param axisType which axis to distribute items along 56 | * @param fixedSpacing the spacing between each item 57 | * @param leadSpacing the spacing before the first item and the container 58 | * @param tailSpacing the spacing after the last item and the container 59 | */ 60 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 61 | 62 | /** 63 | * distribute with fixed item size 64 | * 65 | * @param axisType which axis to distribute items along 66 | * @param fixedItemLength the fixed length of each item 67 | * @param leadSpacing the spacing before the first item and the container 68 | * @param tailSpacing the spacing after the last item and the container 69 | */ 70 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; 71 | 72 | @end 73 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/NSArray+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASAdditions.m 3 | // 4 | // 5 | // Created by Daniel Hammond on 11/26/13. 6 | // 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | #import "View+MASAdditions.h" 11 | 12 | @implementation NSArray (MASAdditions) 13 | 14 | - (NSArray *)mas_makeConstraints:(MASConstraintMakerConfigBlock)block { 15 | NSMutableArray *constraints = [NSMutableArray array]; 16 | for (MAS_VIEW *view in self) { 17 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 18 | [constraints addObjectsFromArray:[view mas_makeConstraints:block]]; 19 | } 20 | return constraints; 21 | } 22 | 23 | - (NSArray *)mas_updateConstraints:(MASConstraintMakerConfigBlock)block { 24 | NSMutableArray *constraints = [NSMutableArray array]; 25 | for (MAS_VIEW *view in self) { 26 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 27 | [constraints addObjectsFromArray:[view mas_updateConstraints:block]]; 28 | } 29 | return constraints; 30 | } 31 | 32 | - (NSArray *)mas_remakeConstraints:(MASConstraintMakerConfigBlock)block { 33 | NSMutableArray *constraints = [NSMutableArray array]; 34 | for (MAS_VIEW *view in self) { 35 | NSAssert([view isKindOfClass:[MAS_VIEW class]], @"All objects in the array must be views"); 36 | [constraints addObjectsFromArray:[view mas_remakeConstraints:block]]; 37 | } 38 | return constraints; 39 | } 40 | 41 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 42 | if (self.count < 2) { 43 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 44 | return; 45 | } 46 | 47 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 48 | if (axisType == MASAxisTypeHorizontal) { 49 | MAS_VIEW *prev; 50 | for (int i = 0; i < self.count; i++) { 51 | MAS_VIEW *v = self[i]; 52 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 53 | if (prev) { 54 | make.width.equalTo(prev); 55 | make.left.equalTo(prev.mas_right).offset(fixedSpacing); 56 | if (i == self.count - 1) {//last one 57 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 58 | } 59 | } 60 | else {//first one 61 | make.left.equalTo(tempSuperView).offset(leadSpacing); 62 | } 63 | 64 | }]; 65 | prev = v; 66 | } 67 | } 68 | else { 69 | MAS_VIEW *prev; 70 | for (int i = 0; i < self.count; i++) { 71 | MAS_VIEW *v = self[i]; 72 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 73 | if (prev) { 74 | make.height.equalTo(prev); 75 | make.top.equalTo(prev.mas_bottom).offset(fixedSpacing); 76 | if (i == self.count - 1) {//last one 77 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 78 | } 79 | } 80 | else {//first one 81 | make.top.equalTo(tempSuperView).offset(leadSpacing); 82 | } 83 | 84 | }]; 85 | prev = v; 86 | } 87 | } 88 | } 89 | 90 | - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing { 91 | if (self.count < 2) { 92 | NSAssert(self.count>1,@"views to distribute need to bigger than one"); 93 | return; 94 | } 95 | 96 | MAS_VIEW *tempSuperView = [self mas_commonSuperviewOfViews]; 97 | if (axisType == MASAxisTypeHorizontal) { 98 | MAS_VIEW *prev; 99 | for (int i = 0; i < self.count; i++) { 100 | MAS_VIEW *v = self[i]; 101 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 102 | if (prev) { 103 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 104 | make.width.equalTo(@(fixedItemLength)); 105 | if (i == self.count - 1) {//last one 106 | make.right.equalTo(tempSuperView).offset(-tailSpacing); 107 | } 108 | else { 109 | make.right.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 110 | } 111 | } 112 | else {//first one 113 | make.left.equalTo(tempSuperView).offset(leadSpacing); 114 | make.width.equalTo(@(fixedItemLength)); 115 | } 116 | }]; 117 | prev = v; 118 | } 119 | } 120 | else { 121 | MAS_VIEW *prev; 122 | for (int i = 0; i < self.count; i++) { 123 | MAS_VIEW *v = self[i]; 124 | [v mas_makeConstraints:^(MASConstraintMaker *make) { 125 | if (prev) { 126 | CGFloat offset = (1-(i/((CGFloat)self.count-1)))*(fixedItemLength+leadSpacing)-i*tailSpacing/(((CGFloat)self.count-1)); 127 | make.height.equalTo(@(fixedItemLength)); 128 | if (i == self.count - 1) {//last one 129 | make.bottom.equalTo(tempSuperView).offset(-tailSpacing); 130 | } 131 | else { 132 | make.bottom.equalTo(tempSuperView).multipliedBy(i/((CGFloat)self.count-1)).with.offset(offset); 133 | } 134 | } 135 | else {//first one 136 | make.top.equalTo(tempSuperView).offset(leadSpacing); 137 | make.height.equalTo(@(fixedItemLength)); 138 | } 139 | }]; 140 | prev = v; 141 | } 142 | } 143 | } 144 | 145 | - (MAS_VIEW *)mas_commonSuperviewOfViews 146 | { 147 | MAS_VIEW *commonSuperview = nil; 148 | MAS_VIEW *previousView = nil; 149 | for (id object in self) { 150 | if ([object isKindOfClass:[MAS_VIEW class]]) { 151 | MAS_VIEW *view = (MAS_VIEW *)object; 152 | if (previousView) { 153 | commonSuperview = [view mas_closestCommonSuperview:commonSuperview]; 154 | } else { 155 | commonSuperview = view; 156 | } 157 | previousView = view; 158 | } 159 | } 160 | NSAssert(commonSuperview, @"Can't constrain views that do not share a common superview. Make sure that all the views in this array have been added into the same view hierarchy."); 161 | return commonSuperview; 162 | } 163 | 164 | @end 165 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/NSArray+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSArray+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSArray+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand array additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface NSArray (MASShorthandAdditions) 18 | 19 | - (NSArray *)makeConstraints:(MASConstraintMakerConfigBlock)block; 20 | - (NSArray *)updateConstraints:(MASConstraintMakerConfigBlock)block; 21 | - (NSArray *)remakeConstraints:(MASConstraintMakerConfigBlock)block; 22 | 23 | @end 24 | 25 | @implementation NSArray (MASShorthandAdditions) 26 | 27 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *))block { 28 | return [self mas_makeConstraints:block]; 29 | } 30 | 31 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { 32 | return [self mas_updateConstraints:block]; 33 | } 34 | 35 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *))block { 36 | return [self mas_remakeConstraints:block]; 37 | } 38 | 39 | @end 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/NSLayoutConstraint+MASDebugAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | 11 | /** 12 | * makes debug and log output of NSLayoutConstraints more readable 13 | */ 14 | @interface NSLayoutConstraint (MASDebugAdditions) 15 | 16 | @end 17 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/NSLayoutConstraint+MASDebugAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSLayoutConstraint+MASDebugAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 3/08/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "NSLayoutConstraint+MASDebugAdditions.h" 10 | #import "MASConstraint.h" 11 | #import "MASLayoutConstraint.h" 12 | 13 | @implementation NSLayoutConstraint (MASDebugAdditions) 14 | 15 | #pragma mark - description maps 16 | 17 | + (NSDictionary *)layoutRelationDescriptionsByValue { 18 | static dispatch_once_t once; 19 | static NSDictionary *descriptionMap; 20 | dispatch_once(&once, ^{ 21 | descriptionMap = @{ 22 | @(NSLayoutRelationEqual) : @"==", 23 | @(NSLayoutRelationGreaterThanOrEqual) : @">=", 24 | @(NSLayoutRelationLessThanOrEqual) : @"<=", 25 | }; 26 | }); 27 | return descriptionMap; 28 | } 29 | 30 | + (NSDictionary *)layoutAttributeDescriptionsByValue { 31 | static dispatch_once_t once; 32 | static NSDictionary *descriptionMap; 33 | dispatch_once(&once, ^{ 34 | descriptionMap = @{ 35 | @(NSLayoutAttributeTop) : @"top", 36 | @(NSLayoutAttributeLeft) : @"left", 37 | @(NSLayoutAttributeBottom) : @"bottom", 38 | @(NSLayoutAttributeRight) : @"right", 39 | @(NSLayoutAttributeLeading) : @"leading", 40 | @(NSLayoutAttributeTrailing) : @"trailing", 41 | @(NSLayoutAttributeWidth) : @"width", 42 | @(NSLayoutAttributeHeight) : @"height", 43 | @(NSLayoutAttributeCenterX) : @"centerX", 44 | @(NSLayoutAttributeCenterY) : @"centerY", 45 | @(NSLayoutAttributeBaseline) : @"baseline", 46 | 47 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 48 | @(NSLayoutAttributeFirstBaseline) : @"firstBaseline", 49 | @(NSLayoutAttributeLastBaseline) : @"lastBaseline", 50 | #endif 51 | 52 | #if TARGET_OS_IPHONE || TARGET_OS_TV 53 | @(NSLayoutAttributeLeftMargin) : @"leftMargin", 54 | @(NSLayoutAttributeRightMargin) : @"rightMargin", 55 | @(NSLayoutAttributeTopMargin) : @"topMargin", 56 | @(NSLayoutAttributeBottomMargin) : @"bottomMargin", 57 | @(NSLayoutAttributeLeadingMargin) : @"leadingMargin", 58 | @(NSLayoutAttributeTrailingMargin) : @"trailingMargin", 59 | @(NSLayoutAttributeCenterXWithinMargins) : @"centerXWithinMargins", 60 | @(NSLayoutAttributeCenterYWithinMargins) : @"centerYWithinMargins", 61 | #endif 62 | 63 | }; 64 | 65 | }); 66 | return descriptionMap; 67 | } 68 | 69 | 70 | + (NSDictionary *)layoutPriorityDescriptionsByValue { 71 | static dispatch_once_t once; 72 | static NSDictionary *descriptionMap; 73 | dispatch_once(&once, ^{ 74 | #if TARGET_OS_IPHONE || TARGET_OS_TV 75 | descriptionMap = @{ 76 | @(MASLayoutPriorityDefaultHigh) : @"high", 77 | @(MASLayoutPriorityDefaultLow) : @"low", 78 | @(MASLayoutPriorityDefaultMedium) : @"medium", 79 | @(MASLayoutPriorityRequired) : @"required", 80 | @(MASLayoutPriorityFittingSizeLevel) : @"fitting size", 81 | }; 82 | #elif TARGET_OS_MAC 83 | descriptionMap = @{ 84 | @(MASLayoutPriorityDefaultHigh) : @"high", 85 | @(MASLayoutPriorityDragThatCanResizeWindow) : @"drag can resize window", 86 | @(MASLayoutPriorityDefaultMedium) : @"medium", 87 | @(MASLayoutPriorityWindowSizeStayPut) : @"window size stay put", 88 | @(MASLayoutPriorityDragThatCannotResizeWindow) : @"drag cannot resize window", 89 | @(MASLayoutPriorityDefaultLow) : @"low", 90 | @(MASLayoutPriorityFittingSizeCompression) : @"fitting size", 91 | @(MASLayoutPriorityRequired) : @"required", 92 | }; 93 | #endif 94 | }); 95 | return descriptionMap; 96 | } 97 | 98 | #pragma mark - description override 99 | 100 | + (NSString *)descriptionForObject:(id)obj { 101 | if ([obj respondsToSelector:@selector(mas_key)] && [obj mas_key]) { 102 | return [NSString stringWithFormat:@"%@:%@", [obj class], [obj mas_key]]; 103 | } 104 | return [NSString stringWithFormat:@"%@:%p", [obj class], obj]; 105 | } 106 | 107 | - (NSString *)description { 108 | NSMutableString *description = [[NSMutableString alloc] initWithString:@"<"]; 109 | 110 | [description appendString:[self.class descriptionForObject:self]]; 111 | 112 | [description appendFormat:@" %@", [self.class descriptionForObject:self.firstItem]]; 113 | if (self.firstAttribute != NSLayoutAttributeNotAnAttribute) { 114 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.firstAttribute)]]; 115 | } 116 | 117 | [description appendFormat:@" %@", self.class.layoutRelationDescriptionsByValue[@(self.relation)]]; 118 | 119 | if (self.secondItem) { 120 | [description appendFormat:@" %@", [self.class descriptionForObject:self.secondItem]]; 121 | } 122 | if (self.secondAttribute != NSLayoutAttributeNotAnAttribute) { 123 | [description appendFormat:@".%@", self.class.layoutAttributeDescriptionsByValue[@(self.secondAttribute)]]; 124 | } 125 | 126 | if (self.multiplier != 1) { 127 | [description appendFormat:@" * %g", self.multiplier]; 128 | } 129 | 130 | if (self.secondAttribute == NSLayoutAttributeNotAnAttribute) { 131 | [description appendFormat:@" %g", self.constant]; 132 | } else { 133 | if (self.constant) { 134 | [description appendFormat:@" %@ %g", (self.constant < 0 ? @"-" : @"+"), ABS(self.constant)]; 135 | } 136 | } 137 | 138 | if (self.priority != MASLayoutPriorityRequired) { 139 | [description appendFormat:@" ^%@", self.class.layoutPriorityDescriptionsByValue[@(self.priority)] ?: [NSNumber numberWithDouble:self.priority]]; 140 | } 141 | 142 | [description appendString:@">"]; 143 | return description; 144 | } 145 | 146 | @end 147 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/View+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | /** 14 | * Provides constraint maker block 15 | * and convience methods for creating MASViewAttribute which are view + NSLayoutAttribute pairs 16 | */ 17 | @interface MAS_VIEW (MASAdditions) 18 | 19 | /** 20 | * following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute 21 | */ 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_left; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_top; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_right; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leading; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_width; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_height; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX; 31 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY; 32 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline; 33 | @property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr); 34 | 35 | #if TARGET_OS_IPHONE || TARGET_OS_TV 36 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leftMargin; 37 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_rightMargin; 38 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topMargin; 39 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomMargin; 40 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_leadingMargin; 41 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_trailingMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerXWithinMargins; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_centerYWithinMargins; 44 | #endif 45 | 46 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 47 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_firstBaseline; 48 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_lastBaseline; 49 | #endif 50 | 51 | /** 52 | * a key to associate with this view 53 | */ 54 | @property (nonatomic, strong) id mas_key; 55 | 56 | /** 57 | * Finds the closest common superview between this view and another view 58 | * 59 | * @param view other view 60 | * 61 | * @return returns nil if common superview could not be found 62 | */ 63 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view; 64 | 65 | /** 66 | * Creates a MASConstraintMaker with the callee view. 67 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing 68 | * 69 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 70 | * 71 | * @return Array of created MASConstraints 72 | */ 73 | - (NSArray *)mas_makeConstraints:(MASConstraintMakerConfigBlock)block; 74 | 75 | /** 76 | * Creates a MASConstraintMaker with the callee view. 77 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 78 | * If an existing constraint exists then it will be updated instead. 79 | * 80 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 81 | * 82 | * @return Array of created/updated MASConstraints 83 | */ 84 | - (NSArray *)mas_updateConstraints:(MASConstraintMakerConfigBlock)block; 85 | 86 | /** 87 | * Creates a MASConstraintMaker with the callee view. 88 | * Any constraints defined are added to the view or the appropriate superview once the block has finished executing. 89 | * All constraints previously installed for the view will be removed. 90 | * 91 | * @param block scope within which you can build up the constraints which you wish to apply to the view. 92 | * 93 | * @return Array of created/updated MASConstraints 94 | */ 95 | - (NSArray *)mas_remakeConstraints:(MASConstraintMakerConfigBlock)block; 96 | 97 | @end 98 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/View+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 20/07/13. 6 | // Copyright (c) 2013 cloudling. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | #import 11 | 12 | @implementation MAS_VIEW (MASAdditions) 13 | 14 | /** 15 | * 用户根据Block配置MASConstraintMaker一些MASConstraint属性,然后调用Install方法进行约束的加载 16 | * 17 | * @param block 属性配置Block 18 | * 19 | * @return 被添加的约束数组(MASConstraint数组) 20 | */ 21 | //MASConstraintMakerConfigBlock 22 | 23 | 24 | //新建约束并添加 25 | - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { 26 | //关闭自动添加约束,我们要手动添加 27 | self.translatesAutoresizingMaskIntoConstraints = NO; 28 | 29 | //创建ConstraintMaker 30 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 31 | 32 | //给maker中的各种成员属性赋值,通过Block进行值的回调,此处的Block就是钩取用户的数据的钩子(参考设计模式中的“好莱坞原则”) 33 | block(constraintMaker); 34 | 35 | //进行约束添加,并返回所Install的约束数组(Array) 36 | return [constraintMaker install]; 37 | } 38 | 39 | //更新约束,updateExisting默认为NO, 更新约束时要设置为YES 40 | - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block { 41 | self.translatesAutoresizingMaskIntoConstraints = NO; 42 | 43 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 44 | 45 | //打开更新开关 46 | constraintMaker.updateExisting = YES; 47 | 48 | block(constraintMaker); 49 | 50 | return [constraintMaker install]; 51 | } 52 | 53 | //重新添加约束,removeExisting默认为NO, 重新添加约束时要设置成YES, 会将原来的约束进行移除并重新添加 54 | - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *))block { 55 | self.translatesAutoresizingMaskIntoConstraints = NO; 56 | 57 | MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; 58 | 59 | //打开移除约束开关 60 | constraintMaker.removeExisting = YES; 61 | 62 | block(constraintMaker); 63 | 64 | return [constraintMaker install]; 65 | } 66 | 67 | #pragma mark - NSLayoutAttribute properties --- 成员属性的Get方法,创建MASViewAttribute对象 68 | - (MASViewAttribute *)mas_left { 69 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeft]; 70 | } 71 | 72 | - (MASViewAttribute *)mas_top { 73 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTop]; 74 | } 75 | 76 | - (MASViewAttribute *)mas_right { 77 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRight]; 78 | } 79 | 80 | - (MASViewAttribute *)mas_bottom { 81 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottom]; 82 | } 83 | 84 | - (MASViewAttribute *)mas_leading { 85 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeading]; 86 | } 87 | 88 | - (MASViewAttribute *)mas_trailing { 89 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailing]; 90 | } 91 | 92 | - (MASViewAttribute *)mas_width { 93 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeWidth]; 94 | } 95 | 96 | - (MASViewAttribute *)mas_height { 97 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeHeight]; 98 | } 99 | 100 | - (MASViewAttribute *)mas_centerX { 101 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterX]; 102 | } 103 | 104 | - (MASViewAttribute *)mas_centerY { 105 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterY]; 106 | } 107 | 108 | - (MASViewAttribute *)mas_baseline { 109 | 110 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBaseline]; 111 | 112 | //你也可以调用这个方法 113 | return self.mas_attribute(NSLayoutAttributeBaseline); 114 | } 115 | 116 | //创建你指定的NSLayoutAttribute,上面那些成员的初始化都可以调用下方的函数完成 117 | - (MASViewAttribute *(^)(NSLayoutAttribute))mas_attribute 118 | { 119 | return ^(NSLayoutAttribute attr) { 120 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:attr]; 121 | }; 122 | } 123 | 124 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 125 | 126 | - (MASViewAttribute *)mas_firstBaseline { 127 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeFirstBaseline]; 128 | } 129 | - (MASViewAttribute *)mas_lastBaseline { 130 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLastBaseline]; 131 | } 132 | 133 | #endif 134 | 135 | #if TARGET_OS_IPHONE || TARGET_OS_TV 136 | 137 | - (MASViewAttribute *)mas_leftMargin { 138 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeftMargin]; 139 | } 140 | 141 | - (MASViewAttribute *)mas_rightMargin { 142 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeRightMargin]; 143 | } 144 | 145 | - (MASViewAttribute *)mas_topMargin { 146 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTopMargin]; 147 | } 148 | 149 | - (MASViewAttribute *)mas_bottomMargin { 150 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeBottomMargin]; 151 | } 152 | 153 | - (MASViewAttribute *)mas_leadingMargin { 154 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeLeadingMargin]; 155 | } 156 | 157 | - (MASViewAttribute *)mas_trailingMargin { 158 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeTrailingMargin]; 159 | } 160 | 161 | - (MASViewAttribute *)mas_centerXWithinMargins { 162 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterXWithinMargins]; 163 | } 164 | 165 | - (MASViewAttribute *)mas_centerYWithinMargins { 166 | return [[MASViewAttribute alloc] initWithView:self layoutAttribute:NSLayoutAttributeCenterYWithinMargins]; 167 | } 168 | 169 | #endif 170 | 171 | #pragma mark - associated properties 172 | 173 | - (id)mas_key { 174 | return objc_getAssociatedObject(self, @selector(mas_key)); 175 | } 176 | 177 | - (void)setMas_key:(id)key { 178 | objc_setAssociatedObject(self, @selector(mas_key), key, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 179 | } 180 | 181 | #pragma mark - heirachy 182 | 183 | //寻找当前视图与参数中的视图的共同父视图,因为约束是添加在父视图上的 184 | - (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view { 185 | MAS_VIEW *closestCommonSuperview = nil; //暂存父视图 186 | MAS_VIEW *secondViewSuperview = view; 187 | while (!closestCommonSuperview && secondViewSuperview) { //遍历secondView的所有父视图 188 | 189 | MAS_VIEW *firstViewSuperview = self; 190 | while (!closestCommonSuperview && firstViewSuperview) { //遍历当前视图的父视图 191 | if (secondViewSuperview == firstViewSuperview) { 192 | closestCommonSuperview = secondViewSuperview; //找到了共同的父视图就结束循环 193 | } 194 | firstViewSuperview = firstViewSuperview.superview; 195 | } 196 | secondViewSuperview = secondViewSuperview.superview; 197 | } 198 | return closestCommonSuperview; //返回共同的父视图 199 | } 200 | 201 | @end 202 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/View+MASShorthandAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+MASShorthandAdditions.h 3 | // Masonry 4 | // 5 | // Created by Jonas Budelmann on 22/07/13. 6 | // Copyright (c) 2013 Jonas Budelmann. All rights reserved. 7 | // 8 | 9 | #import "View+MASAdditions.h" 10 | 11 | #ifdef MAS_SHORTHAND 12 | 13 | /** 14 | * Shorthand view additions without the 'mas_' prefixes, 15 | * only enabled if MAS_SHORTHAND is defined 16 | */ 17 | @interface MAS_VIEW (MASShorthandAdditions) 18 | 19 | @property (nonatomic, strong, readonly) MASViewAttribute *left; 20 | @property (nonatomic, strong, readonly) MASViewAttribute *top; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *right; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *bottom; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *leading; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *trailing; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *width; 26 | @property (nonatomic, strong, readonly) MASViewAttribute *height; 27 | @property (nonatomic, strong, readonly) MASViewAttribute *centerX; 28 | @property (nonatomic, strong, readonly) MASViewAttribute *centerY; 29 | @property (nonatomic, strong, readonly) MASViewAttribute *baseline; 30 | @property (nonatomic, strong, readonly) MASViewAttribute *(^attribute)(NSLayoutAttribute attr); 31 | 32 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 33 | 34 | @property (nonatomic, strong, readonly) MASViewAttribute *firstBaseline; 35 | @property (nonatomic, strong, readonly) MASViewAttribute *lastBaseline; 36 | 37 | #endif 38 | 39 | #if TARGET_OS_IPHONE || TARGET_OS_TV 40 | 41 | @property (nonatomic, strong, readonly) MASViewAttribute *leftMargin; 42 | @property (nonatomic, strong, readonly) MASViewAttribute *rightMargin; 43 | @property (nonatomic, strong, readonly) MASViewAttribute *topMargin; 44 | @property (nonatomic, strong, readonly) MASViewAttribute *bottomMargin; 45 | @property (nonatomic, strong, readonly) MASViewAttribute *leadingMargin; 46 | @property (nonatomic, strong, readonly) MASViewAttribute *trailingMargin; 47 | @property (nonatomic, strong, readonly) MASViewAttribute *centerXWithinMargins; 48 | @property (nonatomic, strong, readonly) MASViewAttribute *centerYWithinMargins; 49 | 50 | #endif 51 | 52 | - (NSArray *)makeConstraints:(MASConstraintMakerConfigBlock)block; 53 | - (NSArray *)updateConstraints:(MASConstraintMakerConfigBlock)block; 54 | - (NSArray *)remakeConstraints:(MASConstraintMakerConfigBlock)block; 55 | 56 | @end 57 | 58 | #define MAS_ATTR_FORWARD(attr) \ 59 | - (MASViewAttribute *)attr { \ 60 | return [self mas_##attr]; \ 61 | } 62 | 63 | @implementation MAS_VIEW (MASShorthandAdditions) 64 | 65 | MAS_ATTR_FORWARD(top); 66 | MAS_ATTR_FORWARD(left); 67 | MAS_ATTR_FORWARD(bottom); 68 | MAS_ATTR_FORWARD(right); 69 | MAS_ATTR_FORWARD(leading); 70 | MAS_ATTR_FORWARD(trailing); 71 | MAS_ATTR_FORWARD(width); 72 | MAS_ATTR_FORWARD(height); 73 | MAS_ATTR_FORWARD(centerX); 74 | MAS_ATTR_FORWARD(centerY); 75 | MAS_ATTR_FORWARD(baseline); 76 | 77 | #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100) 78 | 79 | MAS_ATTR_FORWARD(firstBaseline); 80 | MAS_ATTR_FORWARD(lastBaseline); 81 | 82 | #endif 83 | 84 | #if TARGET_OS_IPHONE || TARGET_OS_TV 85 | 86 | MAS_ATTR_FORWARD(leftMargin); 87 | MAS_ATTR_FORWARD(rightMargin); 88 | MAS_ATTR_FORWARD(topMargin); 89 | MAS_ATTR_FORWARD(bottomMargin); 90 | MAS_ATTR_FORWARD(leadingMargin); 91 | MAS_ATTR_FORWARD(trailingMargin); 92 | MAS_ATTR_FORWARD(centerXWithinMargins); 93 | MAS_ATTR_FORWARD(centerYWithinMargins); 94 | 95 | #endif 96 | 97 | - (MASViewAttribute *(^)(NSLayoutAttribute))attribute { 98 | return [self mas_attribute]; 99 | } 100 | 101 | - (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *))block { 102 | return [self mas_makeConstraints:block]; 103 | } 104 | 105 | - (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *))block { 106 | return [self mas_updateConstraints:block]; 107 | } 108 | 109 | - (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *))block { 110 | return [self mas_remakeConstraints:block]; 111 | } 112 | 113 | @end 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/ViewController+MASAdditions.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.h 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "MASUtilities.h" 10 | #import "MASConstraintMaker.h" 11 | #import "MASViewAttribute.h" 12 | 13 | #ifdef MAS_VIEW_CONTROLLER 14 | 15 | @interface MAS_VIEW_CONTROLLER (MASAdditions) 16 | 17 | /** 18 | * following properties return a new MASViewAttribute with appropriate UILayoutGuide and NSLayoutAttribute 19 | */ 20 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuide; 21 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuide; 22 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideTop; 23 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_topLayoutGuideBottom; 24 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideTop; 25 | @property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideBottom; 26 | 27 | 28 | @end 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /MasonryDemo/Masonry/ViewController+MASAdditions.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIViewController+MASAdditions.m 3 | // Masonry 4 | // 5 | // Created by Craig Siemens on 2015-06-23. 6 | // 7 | // 8 | 9 | #import "ViewController+MASAdditions.h" 10 | 11 | #ifdef MAS_VIEW_CONTROLLER 12 | 13 | @implementation MAS_VIEW_CONTROLLER (MASAdditions) 14 | 15 | - (MASViewAttribute *)mas_topLayoutGuide { 16 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 17 | } 18 | - (MASViewAttribute *)mas_topLayoutGuideTop { 19 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 20 | } 21 | - (MASViewAttribute *)mas_topLayoutGuideBottom { 22 | return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 23 | } 24 | 25 | - (MASViewAttribute *)mas_bottomLayoutGuide { 26 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 27 | } 28 | - (MASViewAttribute *)mas_bottomLayoutGuideTop { 29 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop]; 30 | } 31 | - (MASViewAttribute *)mas_bottomLayoutGuideBottom { 32 | return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeBottom]; 33 | } 34 | 35 | 36 | 37 | @end 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | F414E2B11CD997AD00B0C035 /* heart.png in Resources */ = {isa = PBXBuildFile; fileRef = F414E2B01CD997AD00B0C035 /* heart.png */; }; 11 | F414E2B41CD99E1000B0C035 /* UpdateArrayViews.m in Sources */ = {isa = PBXBuildFile; fileRef = F414E2B31CD99E1000B0C035 /* UpdateArrayViews.m */; }; 12 | F414E2B71CD9BCB600B0C035 /* UserMarginView.m in Sources */ = {isa = PBXBuildFile; fileRef = F414E2B61CD9BCB600B0C035 /* UserMarginView.m */; }; 13 | F414E2BA1CD9C06F00B0C035 /* DistributeView.m in Sources */ = {isa = PBXBuildFile; fileRef = F414E2B91CD9C06F00B0C035 /* DistributeView.m */; }; 14 | F4E55C331CD87CC000AB06BE /* UpdateConstraintView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E55C321CD87CC000AB06BE /* UpdateConstraintView.m */; }; 15 | F4E55C361CD8881C00AB06BE /* RemakeConstraintView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E55C351CD8881C00AB06BE /* RemakeConstraintView.m */; }; 16 | F4E55C391CD88C5900AB06BE /* UseConstantsView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E55C381CD88C5900AB06BE /* UseConstantsView.m */; }; 17 | F4E55C3C1CD88ED100AB06BE /* UseEdgesInsetView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E55C3B1CD88ED100AB06BE /* UseEdgesInsetView.m */; }; 18 | F4E55C3F1CD8A43A00AB06BE /* AspectFitWithRatioView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E55C3E1CD8A43A00AB06BE /* AspectFitWithRatioView.m */; }; 19 | F4E55C421CD8B85800AB06BE /* BasicAnimatedView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4E55C411CD8B85800AB06BE /* BasicAnimatedView.m */; }; 20 | F4EAE77A1CD440D700559214 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7791CD440D700559214 /* main.m */; }; 21 | F4EAE77D1CD440D700559214 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE77C1CD440D700559214 /* AppDelegate.m */; }; 22 | F4EAE7851CD440D700559214 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F4EAE7841CD440D700559214 /* Assets.xcassets */; }; 23 | F4EAE7881CD440D700559214 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F4EAE7861CD440D700559214 /* LaunchScreen.storyboard */; }; 24 | F4EAE7AA1CD4410E00559214 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = F4EAE7901CD4410E00559214 /* Info.plist */; }; 25 | F4EAE7AB1CD4410E00559214 /* MASCompositeConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7921CD4410E00559214 /* MASCompositeConstraint.m */; }; 26 | F4EAE7AC1CD4410E00559214 /* MASConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7951CD4410E00559214 /* MASConstraint.m */; }; 27 | F4EAE7AD1CD4410E00559214 /* MASConstraintMaker.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7971CD4410E00559214 /* MASConstraintMaker.m */; }; 28 | F4EAE7AE1CD4410E00559214 /* MASLayoutConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7991CD4410E00559214 /* MASLayoutConstraint.m */; }; 29 | F4EAE7AF1CD4410E00559214 /* MASViewAttribute.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE79D1CD4410E00559214 /* MASViewAttribute.m */; }; 30 | F4EAE7B01CD4410E00559214 /* MASViewConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE79F1CD4410E00559214 /* MASViewConstraint.m */; }; 31 | F4EAE7B11CD4410E00559214 /* NSArray+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7A11CD4410E00559214 /* NSArray+MASAdditions.m */; }; 32 | F4EAE7B21CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7A41CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.m */; }; 33 | F4EAE7B31CD4410E00559214 /* View+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7A61CD4410E00559214 /* View+MASAdditions.m */; }; 34 | F4EAE7B41CD4410E00559214 /* ViewController+MASAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7A91CD4410E00559214 /* ViewController+MASAdditions.m */; }; 35 | F4EAE7BA1CD7417700559214 /* MasonryTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7B91CD7417700559214 /* MasonryTableViewController.m */; }; 36 | F4EAE7BF1CD743CF00559214 /* SubViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7BE1CD743CF00559214 /* SubViewController.m */; }; 37 | F4EAE7C21CD7451500559214 /* BaicView.m in Sources */ = {isa = PBXBuildFile; fileRef = F4EAE7C11CD7451500559214 /* BaicView.m */; }; 38 | /* End PBXBuildFile section */ 39 | 40 | /* Begin PBXFileReference section */ 41 | F414E2B01CD997AD00B0C035 /* heart.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = heart.png; sourceTree = ""; }; 42 | F414E2B21CD99E1000B0C035 /* UpdateArrayViews.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UpdateArrayViews.h; sourceTree = ""; }; 43 | F414E2B31CD99E1000B0C035 /* UpdateArrayViews.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UpdateArrayViews.m; sourceTree = ""; }; 44 | F414E2B51CD9BCB600B0C035 /* UserMarginView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserMarginView.h; sourceTree = ""; }; 45 | F414E2B61CD9BCB600B0C035 /* UserMarginView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UserMarginView.m; sourceTree = ""; }; 46 | F414E2B81CD9C06F00B0C035 /* DistributeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DistributeView.h; sourceTree = ""; }; 47 | F414E2B91CD9C06F00B0C035 /* DistributeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DistributeView.m; sourceTree = ""; }; 48 | F4E55C311CD87CC000AB06BE /* UpdateConstraintView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UpdateConstraintView.h; sourceTree = ""; }; 49 | F4E55C321CD87CC000AB06BE /* UpdateConstraintView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UpdateConstraintView.m; sourceTree = ""; }; 50 | F4E55C341CD8881C00AB06BE /* RemakeConstraintView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RemakeConstraintView.h; sourceTree = ""; }; 51 | F4E55C351CD8881C00AB06BE /* RemakeConstraintView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RemakeConstraintView.m; sourceTree = ""; }; 52 | F4E55C371CD88C5900AB06BE /* UseConstantsView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UseConstantsView.h; sourceTree = ""; }; 53 | F4E55C381CD88C5900AB06BE /* UseConstantsView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UseConstantsView.m; sourceTree = ""; }; 54 | F4E55C3A1CD88ED100AB06BE /* UseEdgesInsetView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UseEdgesInsetView.h; sourceTree = ""; }; 55 | F4E55C3B1CD88ED100AB06BE /* UseEdgesInsetView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UseEdgesInsetView.m; sourceTree = ""; }; 56 | F4E55C3D1CD8A43A00AB06BE /* AspectFitWithRatioView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AspectFitWithRatioView.h; sourceTree = ""; }; 57 | F4E55C3E1CD8A43A00AB06BE /* AspectFitWithRatioView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AspectFitWithRatioView.m; sourceTree = ""; }; 58 | F4E55C401CD8B85800AB06BE /* BasicAnimatedView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BasicAnimatedView.h; sourceTree = ""; }; 59 | F4E55C411CD8B85800AB06BE /* BasicAnimatedView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BasicAnimatedView.m; sourceTree = ""; }; 60 | F4EAE7751CD440D700559214 /* MasonryDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MasonryDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 61 | F4EAE7791CD440D700559214 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 62 | F4EAE77B1CD440D700559214 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 63 | F4EAE77C1CD440D700559214 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 64 | F4EAE7841CD440D700559214 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 65 | F4EAE7871CD440D700559214 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 66 | F4EAE7891CD440D700559214 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 67 | F4EAE7901CD4410E00559214 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 68 | F4EAE7911CD4410E00559214 /* MASCompositeConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASCompositeConstraint.h; sourceTree = ""; }; 69 | F4EAE7921CD4410E00559214 /* MASCompositeConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASCompositeConstraint.m; sourceTree = ""; }; 70 | F4EAE7931CD4410E00559214 /* MASConstraint+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MASConstraint+Private.h"; sourceTree = ""; }; 71 | F4EAE7941CD4410E00559214 /* MASConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASConstraint.h; sourceTree = ""; }; 72 | F4EAE7951CD4410E00559214 /* MASConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASConstraint.m; sourceTree = ""; }; 73 | F4EAE7961CD4410E00559214 /* MASConstraintMaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASConstraintMaker.h; sourceTree = ""; }; 74 | F4EAE7971CD4410E00559214 /* MASConstraintMaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASConstraintMaker.m; sourceTree = ""; }; 75 | F4EAE7981CD4410E00559214 /* MASLayoutConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASLayoutConstraint.h; sourceTree = ""; }; 76 | F4EAE7991CD4410E00559214 /* MASLayoutConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASLayoutConstraint.m; sourceTree = ""; }; 77 | F4EAE79A1CD4410E00559214 /* Masonry.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Masonry.h; sourceTree = ""; }; 78 | F4EAE79B1CD4410E00559214 /* MASUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASUtilities.h; sourceTree = ""; }; 79 | F4EAE79C1CD4410E00559214 /* MASViewAttribute.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASViewAttribute.h; sourceTree = ""; }; 80 | F4EAE79D1CD4410E00559214 /* MASViewAttribute.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASViewAttribute.m; sourceTree = ""; }; 81 | F4EAE79E1CD4410E00559214 /* MASViewConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASViewConstraint.h; sourceTree = ""; }; 82 | F4EAE79F1CD4410E00559214 /* MASViewConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASViewConstraint.m; sourceTree = ""; }; 83 | F4EAE7A01CD4410E00559214 /* NSArray+MASAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+MASAdditions.h"; sourceTree = ""; }; 84 | F4EAE7A11CD4410E00559214 /* NSArray+MASAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSArray+MASAdditions.m"; sourceTree = ""; }; 85 | F4EAE7A21CD4410E00559214 /* NSArray+MASShorthandAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSArray+MASShorthandAdditions.h"; sourceTree = ""; }; 86 | F4EAE7A31CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSLayoutConstraint+MASDebugAdditions.h"; sourceTree = ""; }; 87 | F4EAE7A41CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSLayoutConstraint+MASDebugAdditions.m"; sourceTree = ""; }; 88 | F4EAE7A51CD4410E00559214 /* View+MASAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "View+MASAdditions.h"; sourceTree = ""; }; 89 | F4EAE7A61CD4410E00559214 /* View+MASAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "View+MASAdditions.m"; sourceTree = ""; }; 90 | F4EAE7A71CD4410E00559214 /* View+MASShorthandAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "View+MASShorthandAdditions.h"; sourceTree = ""; }; 91 | F4EAE7A81CD4410E00559214 /* ViewController+MASAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ViewController+MASAdditions.h"; sourceTree = ""; }; 92 | F4EAE7A91CD4410E00559214 /* ViewController+MASAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "ViewController+MASAdditions.m"; sourceTree = ""; }; 93 | F4EAE7B81CD7417700559214 /* MasonryTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MasonryTableViewController.h; sourceTree = ""; }; 94 | F4EAE7B91CD7417700559214 /* MasonryTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MasonryTableViewController.m; sourceTree = ""; }; 95 | F4EAE7BD1CD743CF00559214 /* SubViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SubViewController.h; sourceTree = ""; }; 96 | F4EAE7BE1CD743CF00559214 /* SubViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SubViewController.m; sourceTree = ""; }; 97 | F4EAE7C01CD7451500559214 /* BaicView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaicView.h; sourceTree = ""; }; 98 | F4EAE7C11CD7451500559214 /* BaicView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaicView.m; sourceTree = ""; }; 99 | F4EAE7C31CD78CC300559214 /* PrefixHeader.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PrefixHeader.pch; sourceTree = ""; }; 100 | /* End PBXFileReference section */ 101 | 102 | /* Begin PBXFrameworksBuildPhase section */ 103 | F4EAE7721CD440D700559214 /* Frameworks */ = { 104 | isa = PBXFrameworksBuildPhase; 105 | buildActionMask = 2147483647; 106 | files = ( 107 | ); 108 | runOnlyForDeploymentPostprocessing = 0; 109 | }; 110 | /* End PBXFrameworksBuildPhase section */ 111 | 112 | /* Begin PBXGroup section */ 113 | F414E2AF1CD9979B00B0C035 /* Image */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | F414E2B01CD997AD00B0C035 /* heart.png */, 117 | ); 118 | name = Image; 119 | sourceTree = ""; 120 | }; 121 | F4EAE76C1CD440D700559214 = { 122 | isa = PBXGroup; 123 | children = ( 124 | F4EAE78F1CD4410E00559214 /* Masonry */, 125 | F4EAE7771CD440D700559214 /* MasonryDemo */, 126 | F4EAE7761CD440D700559214 /* Products */, 127 | ); 128 | sourceTree = ""; 129 | }; 130 | F4EAE7761CD440D700559214 /* Products */ = { 131 | isa = PBXGroup; 132 | children = ( 133 | F4EAE7751CD440D700559214 /* MasonryDemo.app */, 134 | ); 135 | name = Products; 136 | sourceTree = ""; 137 | }; 138 | F4EAE7771CD440D700559214 /* MasonryDemo */ = { 139 | isa = PBXGroup; 140 | children = ( 141 | F414E2AF1CD9979B00B0C035 /* Image */, 142 | F4EAE7BC1CD7439200559214 /* View */, 143 | F4EAE7BB1CD7433400559214 /* Controller */, 144 | F4EAE77B1CD440D700559214 /* AppDelegate.h */, 145 | F4EAE77C1CD440D700559214 /* AppDelegate.m */, 146 | F4EAE7841CD440D700559214 /* Assets.xcassets */, 147 | F4EAE7861CD440D700559214 /* LaunchScreen.storyboard */, 148 | F4EAE7891CD440D700559214 /* Info.plist */, 149 | F4EAE7781CD440D700559214 /* Supporting Files */, 150 | ); 151 | path = MasonryDemo; 152 | sourceTree = ""; 153 | }; 154 | F4EAE7781CD440D700559214 /* Supporting Files */ = { 155 | isa = PBXGroup; 156 | children = ( 157 | F4EAE7791CD440D700559214 /* main.m */, 158 | F4EAE7C31CD78CC300559214 /* PrefixHeader.pch */, 159 | ); 160 | name = "Supporting Files"; 161 | sourceTree = ""; 162 | }; 163 | F4EAE78F1CD4410E00559214 /* Masonry */ = { 164 | isa = PBXGroup; 165 | children = ( 166 | F4EAE7901CD4410E00559214 /* Info.plist */, 167 | F4EAE79C1CD4410E00559214 /* MASViewAttribute.h */, 168 | F4EAE79D1CD4410E00559214 /* MASViewAttribute.m */, 169 | F4EAE7941CD4410E00559214 /* MASConstraint.h */, 170 | F4EAE7951CD4410E00559214 /* MASConstraint.m */, 171 | F4EAE7931CD4410E00559214 /* MASConstraint+Private.h */, 172 | F4EAE79E1CD4410E00559214 /* MASViewConstraint.h */, 173 | F4EAE79F1CD4410E00559214 /* MASViewConstraint.m */, 174 | F4EAE7911CD4410E00559214 /* MASCompositeConstraint.h */, 175 | F4EAE7921CD4410E00559214 /* MASCompositeConstraint.m */, 176 | F4EAE7961CD4410E00559214 /* MASConstraintMaker.h */, 177 | F4EAE7971CD4410E00559214 /* MASConstraintMaker.m */, 178 | F4EAE7981CD4410E00559214 /* MASLayoutConstraint.h */, 179 | F4EAE7991CD4410E00559214 /* MASLayoutConstraint.m */, 180 | F4EAE79A1CD4410E00559214 /* Masonry.h */, 181 | F4EAE79B1CD4410E00559214 /* MASUtilities.h */, 182 | F4EAE7A01CD4410E00559214 /* NSArray+MASAdditions.h */, 183 | F4EAE7A11CD4410E00559214 /* NSArray+MASAdditions.m */, 184 | F4EAE7A21CD4410E00559214 /* NSArray+MASShorthandAdditions.h */, 185 | F4EAE7A31CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.h */, 186 | F4EAE7A41CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.m */, 187 | F4EAE7A51CD4410E00559214 /* View+MASAdditions.h */, 188 | F4EAE7A61CD4410E00559214 /* View+MASAdditions.m */, 189 | F4EAE7A71CD4410E00559214 /* View+MASShorthandAdditions.h */, 190 | F4EAE7A81CD4410E00559214 /* ViewController+MASAdditions.h */, 191 | F4EAE7A91CD4410E00559214 /* ViewController+MASAdditions.m */, 192 | ); 193 | path = Masonry; 194 | sourceTree = ""; 195 | }; 196 | F4EAE7BB1CD7433400559214 /* Controller */ = { 197 | isa = PBXGroup; 198 | children = ( 199 | F4EAE7B81CD7417700559214 /* MasonryTableViewController.h */, 200 | F4EAE7B91CD7417700559214 /* MasonryTableViewController.m */, 201 | F4EAE7BD1CD743CF00559214 /* SubViewController.h */, 202 | F4EAE7BE1CD743CF00559214 /* SubViewController.m */, 203 | ); 204 | name = Controller; 205 | sourceTree = ""; 206 | }; 207 | F4EAE7BC1CD7439200559214 /* View */ = { 208 | isa = PBXGroup; 209 | children = ( 210 | F4EAE7C01CD7451500559214 /* BaicView.h */, 211 | F4EAE7C11CD7451500559214 /* BaicView.m */, 212 | F4E55C311CD87CC000AB06BE /* UpdateConstraintView.h */, 213 | F4E55C321CD87CC000AB06BE /* UpdateConstraintView.m */, 214 | F4E55C341CD8881C00AB06BE /* RemakeConstraintView.h */, 215 | F4E55C351CD8881C00AB06BE /* RemakeConstraintView.m */, 216 | F4E55C371CD88C5900AB06BE /* UseConstantsView.h */, 217 | F4E55C381CD88C5900AB06BE /* UseConstantsView.m */, 218 | F4E55C3A1CD88ED100AB06BE /* UseEdgesInsetView.h */, 219 | F4E55C3B1CD88ED100AB06BE /* UseEdgesInsetView.m */, 220 | F4E55C3D1CD8A43A00AB06BE /* AspectFitWithRatioView.h */, 221 | F4E55C3E1CD8A43A00AB06BE /* AspectFitWithRatioView.m */, 222 | F4E55C401CD8B85800AB06BE /* BasicAnimatedView.h */, 223 | F4E55C411CD8B85800AB06BE /* BasicAnimatedView.m */, 224 | F414E2B21CD99E1000B0C035 /* UpdateArrayViews.h */, 225 | F414E2B31CD99E1000B0C035 /* UpdateArrayViews.m */, 226 | F414E2B51CD9BCB600B0C035 /* UserMarginView.h */, 227 | F414E2B61CD9BCB600B0C035 /* UserMarginView.m */, 228 | F414E2B81CD9C06F00B0C035 /* DistributeView.h */, 229 | F414E2B91CD9C06F00B0C035 /* DistributeView.m */, 230 | ); 231 | name = View; 232 | sourceTree = ""; 233 | }; 234 | /* End PBXGroup section */ 235 | 236 | /* Begin PBXNativeTarget section */ 237 | F4EAE7741CD440D700559214 /* MasonryDemo */ = { 238 | isa = PBXNativeTarget; 239 | buildConfigurationList = F4EAE78C1CD440D700559214 /* Build configuration list for PBXNativeTarget "MasonryDemo" */; 240 | buildPhases = ( 241 | F4EAE7711CD440D700559214 /* Sources */, 242 | F4EAE7721CD440D700559214 /* Frameworks */, 243 | F4EAE7731CD440D700559214 /* Resources */, 244 | ); 245 | buildRules = ( 246 | ); 247 | dependencies = ( 248 | ); 249 | name = MasonryDemo; 250 | productName = MasonryDemo; 251 | productReference = F4EAE7751CD440D700559214 /* MasonryDemo.app */; 252 | productType = "com.apple.product-type.application"; 253 | }; 254 | /* End PBXNativeTarget section */ 255 | 256 | /* Begin PBXProject section */ 257 | F4EAE76D1CD440D700559214 /* Project object */ = { 258 | isa = PBXProject; 259 | attributes = { 260 | LastUpgradeCheck = 0720; 261 | ORGANIZATIONNAME = zeluli; 262 | TargetAttributes = { 263 | F4EAE7741CD440D700559214 = { 264 | CreatedOnToolsVersion = 7.2.1; 265 | DevelopmentTeam = N5WRHCE748; 266 | }; 267 | }; 268 | }; 269 | buildConfigurationList = F4EAE7701CD440D700559214 /* Build configuration list for PBXProject "MasonryDemo" */; 270 | compatibilityVersion = "Xcode 3.2"; 271 | developmentRegion = English; 272 | hasScannedForEncodings = 0; 273 | knownRegions = ( 274 | en, 275 | Base, 276 | ); 277 | mainGroup = F4EAE76C1CD440D700559214; 278 | productRefGroup = F4EAE7761CD440D700559214 /* Products */; 279 | projectDirPath = ""; 280 | projectRoot = ""; 281 | targets = ( 282 | F4EAE7741CD440D700559214 /* MasonryDemo */, 283 | ); 284 | }; 285 | /* End PBXProject section */ 286 | 287 | /* Begin PBXResourcesBuildPhase section */ 288 | F4EAE7731CD440D700559214 /* Resources */ = { 289 | isa = PBXResourcesBuildPhase; 290 | buildActionMask = 2147483647; 291 | files = ( 292 | F414E2B11CD997AD00B0C035 /* heart.png in Resources */, 293 | F4EAE7AA1CD4410E00559214 /* Info.plist in Resources */, 294 | F4EAE7881CD440D700559214 /* LaunchScreen.storyboard in Resources */, 295 | F4EAE7851CD440D700559214 /* Assets.xcassets in Resources */, 296 | ); 297 | runOnlyForDeploymentPostprocessing = 0; 298 | }; 299 | /* End PBXResourcesBuildPhase section */ 300 | 301 | /* Begin PBXSourcesBuildPhase section */ 302 | F4EAE7711CD440D700559214 /* Sources */ = { 303 | isa = PBXSourcesBuildPhase; 304 | buildActionMask = 2147483647; 305 | files = ( 306 | F4E55C3F1CD8A43A00AB06BE /* AspectFitWithRatioView.m in Sources */, 307 | F4EAE7B21CD4410E00559214 /* NSLayoutConstraint+MASDebugAdditions.m in Sources */, 308 | F4EAE7B01CD4410E00559214 /* MASViewConstraint.m in Sources */, 309 | F4EAE7C21CD7451500559214 /* BaicView.m in Sources */, 310 | F4E55C361CD8881C00AB06BE /* RemakeConstraintView.m in Sources */, 311 | F4EAE7AB1CD4410E00559214 /* MASCompositeConstraint.m in Sources */, 312 | F414E2B71CD9BCB600B0C035 /* UserMarginView.m in Sources */, 313 | F4EAE7B11CD4410E00559214 /* NSArray+MASAdditions.m in Sources */, 314 | F4E55C391CD88C5900AB06BE /* UseConstantsView.m in Sources */, 315 | F4E55C331CD87CC000AB06BE /* UpdateConstraintView.m in Sources */, 316 | F4EAE77D1CD440D700559214 /* AppDelegate.m in Sources */, 317 | F4EAE7AE1CD4410E00559214 /* MASLayoutConstraint.m in Sources */, 318 | F4EAE77A1CD440D700559214 /* main.m in Sources */, 319 | F4EAE7B41CD4410E00559214 /* ViewController+MASAdditions.m in Sources */, 320 | F414E2B41CD99E1000B0C035 /* UpdateArrayViews.m in Sources */, 321 | F4EAE7B31CD4410E00559214 /* View+MASAdditions.m in Sources */, 322 | F4EAE7AF1CD4410E00559214 /* MASViewAttribute.m in Sources */, 323 | F4E55C3C1CD88ED100AB06BE /* UseEdgesInsetView.m in Sources */, 324 | F4EAE7BA1CD7417700559214 /* MasonryTableViewController.m in Sources */, 325 | F4E55C421CD8B85800AB06BE /* BasicAnimatedView.m in Sources */, 326 | F4EAE7BF1CD743CF00559214 /* SubViewController.m in Sources */, 327 | F414E2BA1CD9C06F00B0C035 /* DistributeView.m in Sources */, 328 | F4EAE7AC1CD4410E00559214 /* MASConstraint.m in Sources */, 329 | F4EAE7AD1CD4410E00559214 /* MASConstraintMaker.m in Sources */, 330 | ); 331 | runOnlyForDeploymentPostprocessing = 0; 332 | }; 333 | /* End PBXSourcesBuildPhase section */ 334 | 335 | /* Begin PBXVariantGroup section */ 336 | F4EAE7861CD440D700559214 /* LaunchScreen.storyboard */ = { 337 | isa = PBXVariantGroup; 338 | children = ( 339 | F4EAE7871CD440D700559214 /* Base */, 340 | ); 341 | name = LaunchScreen.storyboard; 342 | sourceTree = ""; 343 | }; 344 | /* End PBXVariantGroup section */ 345 | 346 | /* Begin XCBuildConfiguration section */ 347 | F4EAE78A1CD440D700559214 /* Debug */ = { 348 | isa = XCBuildConfiguration; 349 | buildSettings = { 350 | ALWAYS_SEARCH_USER_PATHS = NO; 351 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 352 | CLANG_CXX_LIBRARY = "libc++"; 353 | CLANG_ENABLE_MODULES = YES; 354 | CLANG_ENABLE_OBJC_ARC = YES; 355 | CLANG_WARN_BOOL_CONVERSION = YES; 356 | CLANG_WARN_CONSTANT_CONVERSION = YES; 357 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 358 | CLANG_WARN_EMPTY_BODY = YES; 359 | CLANG_WARN_ENUM_CONVERSION = YES; 360 | CLANG_WARN_INT_CONVERSION = YES; 361 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 362 | CLANG_WARN_UNREACHABLE_CODE = YES; 363 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 364 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 365 | COPY_PHASE_STRIP = NO; 366 | DEBUG_INFORMATION_FORMAT = dwarf; 367 | ENABLE_STRICT_OBJC_MSGSEND = YES; 368 | ENABLE_TESTABILITY = YES; 369 | GCC_C_LANGUAGE_STANDARD = gnu99; 370 | GCC_DYNAMIC_NO_PIC = NO; 371 | GCC_NO_COMMON_BLOCKS = YES; 372 | GCC_OPTIMIZATION_LEVEL = 0; 373 | GCC_PREPROCESSOR_DEFINITIONS = ( 374 | "DEBUG=1", 375 | "$(inherited)", 376 | ); 377 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 378 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 379 | GCC_WARN_UNDECLARED_SELECTOR = YES; 380 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 381 | GCC_WARN_UNUSED_FUNCTION = YES; 382 | GCC_WARN_UNUSED_VARIABLE = YES; 383 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 384 | MTL_ENABLE_DEBUG_INFO = YES; 385 | ONLY_ACTIVE_ARCH = YES; 386 | SDKROOT = iphoneos; 387 | }; 388 | name = Debug; 389 | }; 390 | F4EAE78B1CD440D700559214 /* Release */ = { 391 | isa = XCBuildConfiguration; 392 | buildSettings = { 393 | ALWAYS_SEARCH_USER_PATHS = NO; 394 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 395 | CLANG_CXX_LIBRARY = "libc++"; 396 | CLANG_ENABLE_MODULES = YES; 397 | CLANG_ENABLE_OBJC_ARC = YES; 398 | CLANG_WARN_BOOL_CONVERSION = YES; 399 | CLANG_WARN_CONSTANT_CONVERSION = YES; 400 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 401 | CLANG_WARN_EMPTY_BODY = YES; 402 | CLANG_WARN_ENUM_CONVERSION = YES; 403 | CLANG_WARN_INT_CONVERSION = YES; 404 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 405 | CLANG_WARN_UNREACHABLE_CODE = YES; 406 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 407 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 408 | COPY_PHASE_STRIP = NO; 409 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 410 | ENABLE_NS_ASSERTIONS = NO; 411 | ENABLE_STRICT_OBJC_MSGSEND = YES; 412 | GCC_C_LANGUAGE_STANDARD = gnu99; 413 | GCC_NO_COMMON_BLOCKS = YES; 414 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 415 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 416 | GCC_WARN_UNDECLARED_SELECTOR = YES; 417 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 418 | GCC_WARN_UNUSED_FUNCTION = YES; 419 | GCC_WARN_UNUSED_VARIABLE = YES; 420 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 421 | MTL_ENABLE_DEBUG_INFO = NO; 422 | SDKROOT = iphoneos; 423 | VALIDATE_PRODUCT = YES; 424 | }; 425 | name = Release; 426 | }; 427 | F4EAE78D1CD440D700559214 /* Debug */ = { 428 | isa = XCBuildConfiguration; 429 | buildSettings = { 430 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 431 | DEVELOPMENT_TEAM = N5WRHCE748; 432 | GCC_INCREASE_PRECOMPILED_HEADER_SHARING = NO; 433 | GCC_PREFIX_HEADER = "$(SRCROOT)/MasonryDemo/PrefixHeader.pch"; 434 | INFOPLIST_FILE = MasonryDemo/Info.plist; 435 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 436 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 437 | PRODUCT_BUNDLE_IDENTIFIER = com.zeluli.MasonryDemo; 438 | PRODUCT_NAME = "$(TARGET_NAME)"; 439 | }; 440 | name = Debug; 441 | }; 442 | F4EAE78E1CD440D700559214 /* Release */ = { 443 | isa = XCBuildConfiguration; 444 | buildSettings = { 445 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 446 | DEVELOPMENT_TEAM = N5WRHCE748; 447 | GCC_INCREASE_PRECOMPILED_HEADER_SHARING = NO; 448 | GCC_PREFIX_HEADER = "$(SRCROOT)/MasonryDemo/PrefixHeader.pch"; 449 | INFOPLIST_FILE = MasonryDemo/Info.plist; 450 | IPHONEOS_DEPLOYMENT_TARGET = 8.0; 451 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 452 | PRODUCT_BUNDLE_IDENTIFIER = com.zeluli.MasonryDemo; 453 | PRODUCT_NAME = "$(TARGET_NAME)"; 454 | }; 455 | name = Release; 456 | }; 457 | /* End XCBuildConfiguration section */ 458 | 459 | /* Begin XCConfigurationList section */ 460 | F4EAE7701CD440D700559214 /* Build configuration list for PBXProject "MasonryDemo" */ = { 461 | isa = XCConfigurationList; 462 | buildConfigurations = ( 463 | F4EAE78A1CD440D700559214 /* Debug */, 464 | F4EAE78B1CD440D700559214 /* Release */, 465 | ); 466 | defaultConfigurationIsVisible = 0; 467 | defaultConfigurationName = Release; 468 | }; 469 | F4EAE78C1CD440D700559214 /* Build configuration list for PBXNativeTarget "MasonryDemo" */ = { 470 | isa = XCConfigurationList; 471 | buildConfigurations = ( 472 | F4EAE78D1CD440D700559214 /* Debug */, 473 | F4EAE78E1CD440D700559214 /* Release */, 474 | ); 475 | defaultConfigurationIsVisible = 0; 476 | defaultConfigurationName = Release; 477 | }; 478 | /* End XCConfigurationList section */ 479 | }; 480 | rootObject = F4EAE76D1CD440D700559214 /* Project object */; 481 | } 482 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/project.xcworkspace/xcshareddata/MasonryDemo.xcscmblueprint: -------------------------------------------------------------------------------- 1 | { 2 | "DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "F40B8EF3BE717D345FE0471AD517916A8EB58821", 3 | "DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : { 4 | 5 | }, 6 | "DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : { 7 | "59B0DD3A0DC0B86D2A06DD4B68CBD1C997876614" : 0, 8 | "6FC4832245A6A8E50F2A782F20A8B58A46BD1FEB" : 0, 9 | "F40B8EF3BE717D345FE0471AD517916A8EB58821" : 0 10 | }, 11 | "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "5C79FFAD-8611-4DC3-A53D-643E1AB44275", 12 | "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { 13 | "59B0DD3A0DC0B86D2A06DD4B68CBD1C997876614" : "", 14 | "6FC4832245A6A8E50F2A782F20A8B58A46BD1FEB" : "Masonry\/", 15 | "F40B8EF3BE717D345FE0471AD517916A8EB58821" : "MasonryDemo\/" 16 | }, 17 | "DVTSourceControlWorkspaceBlueprintNameKey" : "MasonryDemo", 18 | "DVTSourceControlWorkspaceBlueprintVersion" : 204, 19 | "DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "MasonryDemo\/MasonryDemo.xcodeproj", 20 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [ 21 | { 22 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/lizelu\/CollectionViewControllerDemo.git", 23 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 24 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "59B0DD3A0DC0B86D2A06DD4B68CBD1C997876614" 25 | }, 26 | { 27 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/lizelu\/Masonry.git", 28 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 29 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "6FC4832245A6A8E50F2A782F20A8B58A46BD1FEB" 30 | }, 31 | { 32 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/lizelu\/MasonryDemo.git", 33 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 34 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "F40B8EF3BE717D345FE0471AD517916A8EB58821" 35 | } 36 | ] 37 | } -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/project.xcworkspace/xcuserdata/lizelu.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo.xcodeproj/project.xcworkspace/xcuserdata/lizelu.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/xcuserdata/lizelu.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/xcuserdata/lizelu.xcuserdatad/xcschemes/MasonryDemo.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo.xcodeproj/xcuserdata/lizelu.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | MasonryDemo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | F4EAE7741CD440D700559214 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/4/30. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/4/30. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | #import "MasonryTableViewController.h" 11 | 12 | @interface AppDelegate () 13 | 14 | @end 15 | 16 | @implementation AppDelegate 17 | 18 | 19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 20 | 21 | self.window = [[UIWindow alloc] initWithFrame: UIScreen.mainScreen.bounds]; 22 | self.window.backgroundColor = [UIColor whiteColor]; 23 | 24 | UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController: [MasonryTableViewController new]]; 25 | 26 | [self.window setRootViewController:navigationController]; 27 | [self.window makeKeyAndVisible]; 28 | return YES; 29 | } 30 | 31 | - (void)applicationWillResignActive:(UIApplication *)application { 32 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 33 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 34 | } 35 | 36 | - (void)applicationDidEnterBackground:(UIApplication *)application { 37 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 38 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 39 | } 40 | 41 | - (void)applicationWillEnterForeground:(UIApplication *)application { 42 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 43 | } 44 | 45 | - (void)applicationDidBecomeActive:(UIApplication *)application { 46 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 47 | } 48 | 49 | - (void)applicationWillTerminate:(UIApplication *)application { 50 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 51 | } 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/AspectFitWithRatioView.h: -------------------------------------------------------------------------------- 1 | // 2 | // AspectFitWithRatioView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AspectFitWithRatioView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/AspectFitWithRatioView.m: -------------------------------------------------------------------------------- 1 | // 2 | // AspectFitWithRatioView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "AspectFitWithRatioView.h" 10 | 11 | @implementation AspectFitWithRatioView 12 | -(instancetype)init { 13 | self = [super init]; 14 | [self addView]; 15 | return self; 16 | } 17 | 18 | - (void)addView { 19 | UIView *topView = [UIView new]; 20 | topView.backgroundColor = [UIColor grayColor]; 21 | topView.layer.borderWidth = 2; 22 | topView.layer.borderColor = [[UIColor blackColor] CGColor]; 23 | [self addSubview:topView]; 24 | 25 | UIView *topInnerView = [UIView new]; 26 | topInnerView.backgroundColor = [UIColor greenColor]; 27 | topInnerView.layer.borderWidth = 2; 28 | topInnerView.layer.borderColor = [[UIColor whiteColor] CGColor]; 29 | [topView addSubview:topInnerView]; 30 | 31 | 32 | 33 | 34 | UIView *bottomView = [UIView new]; 35 | bottomView.backgroundColor = [UIColor greenColor]; 36 | bottomView.layer.borderWidth = 2; 37 | bottomView.layer.borderColor = [[UIColor blackColor] CGColor]; 38 | [self addSubview:bottomView]; 39 | 40 | UIView *bottomInnerView = [UIView new]; 41 | bottomInnerView.backgroundColor = [UIColor grayColor]; 42 | bottomInnerView.layer.borderWidth = 2; 43 | bottomInnerView.layer.borderColor = [[UIColor blackColor] CGColor]; 44 | [bottomView addSubview:bottomInnerView]; 45 | 46 | 47 | 48 | 49 | [topView mas_makeConstraints:^(MASConstraintMaker *make) { 50 | make.left.right.and.top.equalTo(self); 51 | }]; 52 | 53 | [topInnerView mas_makeConstraints:^(MASConstraintMaker *make) { 54 | make.width.equalTo(topInnerView.mas_height).multipliedBy(3); //width:height = 3:1 55 | 56 | make.height.width.lessThanOrEqualTo(topView); 57 | make.height.width.equalTo(self).priorityLow(); 58 | 59 | make.center.equalTo(topView); 60 | }]; 61 | 62 | 63 | 64 | 65 | [bottomView mas_makeConstraints:^(MASConstraintMaker *make) { 66 | make.left.right.and.bottom.equalTo(self); 67 | make.top.equalTo(topView.mas_bottom); 68 | make.height.equalTo(topView); 69 | }]; 70 | 71 | [bottomInnerView mas_makeConstraints:^(MASConstraintMaker *make) { 72 | make.height.equalTo(bottomInnerView.mas_width).multipliedBy(3); //width:height = 1:3 73 | 74 | make.height.width.lessThanOrEqualTo(bottomView); 75 | make.height.width.equalTo(self).priorityLow(); 76 | 77 | make.center.equalTo(bottomView); 78 | }]; 79 | 80 | } 81 | 82 | 83 | @end 84 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "size" : "29x29", 15 | "idiom" : "iphone", 16 | "filename" : "icon_58.png", 17 | "scale" : "2x" 18 | }, 19 | { 20 | "size" : "29x29", 21 | "idiom" : "iphone", 22 | "filename" : "icon_87.png", 23 | "scale" : "3x" 24 | }, 25 | { 26 | "size" : "40x40", 27 | "idiom" : "iphone", 28 | "filename" : "icon_80.png", 29 | "scale" : "2x" 30 | }, 31 | { 32 | "size" : "40x40", 33 | "idiom" : "iphone", 34 | "filename" : "icon_120-1.png", 35 | "scale" : "3x" 36 | }, 37 | { 38 | "size" : "60x60", 39 | "idiom" : "iphone", 40 | "filename" : "icon_120.png", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "size" : "60x60", 45 | "idiom" : "iphone", 46 | "filename" : "icon_180.png", 47 | "scale" : "3x" 48 | } 49 | ], 50 | "info" : { 51 | "version" : 1, 52 | "author" : "xcode" 53 | } 54 | } -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_120-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_120-1.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_120.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_180.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_180.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_58.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_58.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_80.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_80.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_87.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/Assets.xcassets/AppIcon.appiconset/icon_87.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/BaicView.h: -------------------------------------------------------------------------------- 1 | // 2 | // BaseView.h 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface BaicView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/BaicView.m: -------------------------------------------------------------------------------- 1 | // 2 | // BaicView.m 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "BaicView.h" 10 | 11 | @implementation BaicView 12 | -(instancetype)init { 13 | self = [super init]; 14 | [self addView]; 15 | return self; 16 | } 17 | 18 | - (void)addView { 19 | UIView *greenView = [UIView new]; 20 | greenView.backgroundColor = [UIColor greenColor]; 21 | greenView.layer.borderWidth = 2; 22 | greenView.layer.borderColor = [[UIColor blackColor] CGColor]; 23 | [self addSubview:greenView]; 24 | 25 | UIView *redView = [UIView new]; 26 | redView.backgroundColor = [UIColor redColor]; 27 | redView.layer.borderWidth = 2; 28 | redView.layer.borderColor = [[UIColor blackColor] CGColor]; 29 | [self addSubview:redView]; 30 | 31 | UIView *blueView = [UIView new]; 32 | blueView.backgroundColor = [UIColor blueColor]; 33 | blueView.layer.borderWidth = 2; 34 | blueView.layer.borderColor = [[UIColor blackColor] CGColor]; 35 | [self addSubview:blueView]; 36 | 37 | int padding = 10; 38 | 39 | NSArray *array = [greenView mas_makeConstraints:^(MASConstraintMaker *make) { 40 | make.top.equalTo(@10); //equalTo的参数可以是NSValue类型 41 | make.left.equalTo(self).offset(padding); //equalTo的参数可以是View 42 | make.bottom.equalTo(blueView.mas_top).offset(-padding); 43 | make.right.equalTo(redView.mas_left).offset(-padding); 44 | make.width.equalTo(redView.mas_width); 45 | 46 | make.height.equalTo(@[redView, blueView]); 47 | //make.height.equalTo(blueView.mas_height); 48 | }]; 49 | 50 | NSLog(@"%@", array); 51 | 52 | [redView mas_makeConstraints:^(MASConstraintMaker *make) { 53 | 54 | // make.top.equalTo(self.mas_top).offset(padding); //equalTo的参数是MASViewAttribute 55 | make.left.equalTo(greenView.mas_right).offset(padding); 56 | make.right.equalTo(self.mas_right).offset(-padding); 57 | make.bottom.equalTo(blueView.mas_top).offset(-padding); 58 | make.width.equalTo(greenView.mas_width); 59 | 60 | //make.height.equalTo(@[greenView, blueView]); //equalTo的参数为数组的情况 61 | }]; 62 | 63 | [blueView mas_makeConstraints:^(MASConstraintMaker *make) { 64 | // make.top.equalTo(redView.mas_bottom).offset(padding); 65 | make.left.equalTo(self.mas_left).offset(padding); 66 | make.right.equalTo(self.mas_right).offset(-padding); 67 | make.bottom.equalTo(self.mas_bottom).offset(-padding); 68 | 69 | //make.height.equalTo(@[greenView, redView]); 70 | }]; 71 | 72 | //NSLog(@"%@, %@, %@", greenView.mas_key, redView.mas_key, blueView.mas_key); 73 | NSLog(@"%@", [MASViewConstraint installedConstraintsForView:blueView]); 74 | 75 | UIView *subView = [UIView new]; 76 | [self addSubview:subView]; 77 | UIView *superView = self; 78 | 79 | subView.translatesAutoresizingMaskIntoConstraints = NO; 80 | NSLayoutConstraint * constraint = [NSLayoutConstraint 81 | constraintWithItem:subView //subView 82 | attribute:NSLayoutAttributeTop //subView.top 83 | relatedBy:NSLayoutRelationEqual //subView.top = 84 | toItem:superView //subView.top = superView 85 | attribute:NSLayoutAttributeTop //subView.top = superView.top 86 | multiplier:1.0 //subView.top = superView.top * 1 87 | constant:10]; //subView.top = superView.top * 1 + 10 88 | [subView addConstraint:constraint]; 89 | 90 | 91 | 92 | [subView mas_makeConstraints:^(MASConstraintMaker *make) { 93 | 94 | make.top.equalTo(@10); //subView.top = 10 //默认是相对于父视图,倍数为1 95 | 96 | //等价于 97 | make.top.equalTo(superView.mas_top).offset(10); //subView.top = superView.top + 10 98 | 99 | //等价于 100 | make.top.equalTo(superView.mas_top).multipliedBy(1).offset(10); //subView.top = superView.top * 1 + 10 101 | 102 | }]; 103 | 104 | 105 | 106 | } 107 | 108 | @end 109 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/BasicAnimatedView.h: -------------------------------------------------------------------------------- 1 | // 2 | // BasicAnimatedView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface BasicAnimatedView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/BasicAnimatedView.m: -------------------------------------------------------------------------------- 1 | // 2 | // BasicAnimatedView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "BasicAnimatedView.h" 10 | 11 | @interface BasicAnimatedView() 12 | 13 | @property (nonatomic, assign) int width; 14 | @property (nonatomic, strong) UIView *view; 15 | 16 | @end 17 | 18 | @implementation BasicAnimatedView 19 | -(instancetype)init { 20 | self = [super init]; 21 | [self addView]; 22 | return self; 23 | } 24 | 25 | - (void)addView { 26 | UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heart"]]; 27 | [imageView setContentMode:UIViewContentModeScaleAspectFit]; 28 | [self addSubview:imageView]; 29 | 30 | self.width = 50; 31 | [imageView mas_makeConstraints:^(MASConstraintMaker *make) { 32 | make.width.equalTo(@(self.width)); 33 | make.height.equalTo(imageView.mas_width); 34 | make.centerY.equalTo(self).offset(-30); 35 | make.centerX.equalTo(self); 36 | }]; 37 | 38 | _view = imageView; 39 | 40 | 41 | } 42 | 43 | #pragma - mark 视图回调 44 | //视图加载后开始动画 45 | - (void)didMoveToWindow { 46 | [self animateWithInvertedInsets:NO]; 47 | } 48 | 49 | 50 | - (void)animateWithInvertedInsets:(BOOL)invertedInsets { 51 | self.width = invertedInsets ? 50 : 200; 52 | 53 | //更新约束 54 | [_view mas_updateConstraints:^(MASConstraintMaker *make) { 55 | make.width.equalTo(@(self.width)); 56 | }]; 57 | 58 | [UIView animateWithDuration:1 animations:^{ 59 | [self layoutIfNeeded]; 60 | } completion:^(BOOL finished) { 61 | //repeat! 62 | [self animateWithInvertedInsets:!invertedInsets]; 63 | }]; 64 | 65 | } 66 | 67 | 68 | 69 | @end 70 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/DistributeView.h: -------------------------------------------------------------------------------- 1 | // 2 | // DistributeView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/4. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DistributeView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/DistributeView.m: -------------------------------------------------------------------------------- 1 | // 2 | // DistributeView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/4. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "DistributeView.h" 10 | 11 | @implementation DistributeView 12 | 13 | - (instancetype)init { 14 | self = [super init]; 15 | 16 | UIView *view = UIView.new; 17 | view.backgroundColor = [UIColor greenColor]; 18 | view.layer.borderColor = UIColor.blackColor.CGColor; 19 | view.layer.borderWidth = 2; 20 | [self addSubview:view]; 21 | 22 | [view mas_makeConstraints:^(MASConstraintMaker *make) { 23 | }]; 24 | 25 | 26 | return self; 27 | } 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | Masonry介绍 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1 25 | LSRequiresIPhoneOS 26 | 27 | UILaunchStoryboardName 28 | LaunchScreen 29 | UIMainStoryboardFile 30 | LaunchScreen 31 | UIRequiredDeviceCapabilities 32 | 33 | armv7 34 | 35 | UISupportedInterfaceOrientations 36 | 37 | UIInterfaceOrientationPortrait 38 | UIInterfaceOrientationLandscapeLeft 39 | UIInterfaceOrientationLandscapeRight 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/MasonryTableViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // MasonryTableViewController.h 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface MasonryTableViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/MasonryTableViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // MasonryTableViewController.m 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "MasonryTableViewController.h" 10 | #import "SubViewController.h" 11 | #import "BaicView.h" 12 | #import "UpdateConstraintView.h" 13 | #import "RemakeConstraintView.h" 14 | #import "UseConstantsView.h" 15 | #import "UseEdgesInsetView.h" 16 | #import "AspectFitWithRatioView.h" 17 | #import "BasicAnimatedView.h" 18 | #import "UpdateArrayViews.h" 19 | #import "UserMarginView.h" 20 | 21 | static NSString * const CellReuseIdentifier = @"kCellReuseIdentifier"; 22 | 23 | @interface MasonryTableViewController () 24 | 25 | @property (nonatomic, strong) NSArray *viewClasses; 26 | @property (nonatomic, strong) NSDictionary *cellTitles; 27 | 28 | @end 29 | 30 | @implementation MasonryTableViewController 31 | 32 | -(instancetype)init { 33 | if (self == nil) { 34 | self = [super init]; 35 | } 36 | self.title = @"示例"; 37 | self.viewClasses = @[BaicView.class, 38 | UpdateConstraintView.class, 39 | RemakeConstraintView.class, 40 | UseConstantsView.class, 41 | UseEdgesInsetView.class, 42 | AspectFitWithRatioView.class, 43 | BasicAnimatedView.class, 44 | UpdateArrayViews.class, 45 | UserMarginView.class]; 46 | 47 | self.cellTitles = @{NSStringFromClass(BaicView.class): @"基本布局", 48 | NSStringFromClass(UpdateConstraintView.class): @"约束更新", 49 | NSStringFromClass(RemakeConstraintView.class): @"重加约束", 50 | NSStringFromClass(UseConstantsView.class): @"使用链式调用添加常量约束值", 51 | NSStringFromClass(UseEdgesInsetView.class): @"使用内边距", 52 | NSStringFromClass(AspectFitWithRatioView.class): @"使用宽高比", 53 | NSStringFromClass(BasicAnimatedView.class): @"基本动画", 54 | NSStringFromClass(UpdateArrayViews.class): @"使用数组更新动画", 55 | NSStringFromClass(UserMarginView.class): @"使用外边距"}; 56 | 57 | return self; 58 | } 59 | 60 | - (void)viewDidLoad { 61 | [super viewDidLoad]; 62 | [self.tableView registerClass:UITableViewCell.class forCellReuseIdentifier:CellReuseIdentifier]; 63 | 64 | } 65 | 66 | -(void)viewWillAppear:(BOOL)animated { 67 | [self.tableView reloadData]; 68 | } 69 | 70 | #pragma mark - Table view data source 71 | 72 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 73 | return 1; 74 | } 75 | 76 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 77 | return self.viewClasses.count; 78 | } 79 | 80 | 81 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 82 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIdentifier forIndexPath:indexPath]; 83 | cell.textLabel.text = self.cellTitles[NSStringFromClass(self.viewClasses[indexPath.row])]; 84 | 85 | return cell; 86 | } 87 | 88 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 89 | return 50.0f; 90 | } 91 | 92 | -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 93 | Class viewClass = self.viewClasses[indexPath.row]; 94 | NSString *title = self.cellTitles[NSStringFromClass(self.viewClasses[indexPath.row])]; 95 | SubViewController *subViewController = [[SubViewController alloc] initWithTitle:title viewClass:viewClass]; 96 | [self.navigationController pushViewController:subViewController animated:YES]; 97 | } 98 | 99 | - (void)didReceiveMemoryWarning { 100 | [super didReceiveMemoryWarning]; 101 | } 102 | 103 | 104 | @end 105 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/PrefixHeader.pch: -------------------------------------------------------------------------------- 1 | // 2 | // PrefixHeader.pch 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #ifndef PrefixHeader_pch 10 | #define PrefixHeader_pch 11 | 12 | // Include any system framework and library headers here that should be included in all compilation units. 13 | // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. 14 | #import "Masonry.h" 15 | 16 | #endif /* PrefixHeader_pch */ 17 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/RemakeConstraintView.h: -------------------------------------------------------------------------------- 1 | // 2 | // RemakeConstraintView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface RemakeConstraintView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/RemakeConstraintView.m: -------------------------------------------------------------------------------- 1 | // 2 | // RemakeConstraintView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "RemakeConstraintView.h" 10 | 11 | @interface RemakeConstraintView() 12 | @property (nonatomic, strong) UIButton * button; 13 | @property (nonatomic, assign) CGPoint buttonCenter; 14 | 15 | @end 16 | 17 | @implementation RemakeConstraintView 18 | 19 | -(instancetype)init { 20 | self = [super init]; 21 | self.buttonCenter = CGPointMake(-100, -100); 22 | [self addView]; 23 | return self; 24 | } 25 | 26 | - (void)addView { 27 | self.button = [[UIButton alloc] init]; 28 | [self.button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside]; 29 | self.button.backgroundColor = [UIColor redColor]; 30 | self.button.layer.borderWidth = 5; 31 | self.button.layer.borderColor = [[UIColor blackColor] CGColor]; 32 | [self.button setTitle:@"重加约束" forState:UIControlStateNormal]; 33 | [self addSubview:self.button]; 34 | 35 | } 36 | 37 | 38 | //添加这句话就会执行 updateConstraints方法 39 | + (BOOL)requiresConstraintBasedLayout { 40 | return YES; 41 | } 42 | 43 | //重新该更新约束的方法 -- mas_remakeConstraints 44 | -(void)updateConstraints { 45 | [self.button mas_remakeConstraints:^(MASConstraintMaker *make) { 46 | make.center.mas_equalTo(self.buttonCenter); 47 | make.size.mas_equalTo(CGSizeMake(100, 100)); 48 | }]; 49 | [super updateConstraints]; 50 | } 51 | 52 | - (void)tapButton:(UIButton *)sender { 53 | static int i = 0; 54 | if (i == 0) { 55 | i = 1; 56 | self.buttonCenter = CGPointMake(100, 100); 57 | } else { 58 | i = 0; 59 | self.buttonCenter = CGPointMake(-100, -100); 60 | } 61 | 62 | // 告诉约束需要更新 63 | [self setNeedsUpdateConstraints]; 64 | 65 | // 更新约束 66 | [self updateConstraintsIfNeeded]; 67 | 68 | [UIView animateWithDuration:0.4 animations:^{ 69 | [self layoutIfNeeded]; 70 | }]; 71 | } 72 | 73 | @end 74 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/SubViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // SubViewController.h 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SubViewController : UIViewController 12 | - (instancetype)initWithTitle:(NSString *)title viewClass:(Class)viewClass; 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/SubViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // SubViewController.m 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/5/2. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "SubViewController.h" 10 | 11 | @interface SubViewController () 12 | @property (nonatomic, strong) Class viewClass; 13 | @end 14 | 15 | @implementation SubViewController 16 | 17 | - (instancetype)initWithTitle:(NSString *)title viewClass:(Class)viewClass { 18 | if (self == nil) { 19 | self = [super init]; 20 | } 21 | 22 | self.title = title; 23 | self.viewClass = viewClass; 24 | 25 | return self; 26 | } 27 | 28 | -(void)loadView { 29 | self.view = self.viewClass.new; 30 | self.view.backgroundColor = [UIColor whiteColor]; 31 | } 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UpdateArrayViews.h: -------------------------------------------------------------------------------- 1 | // 2 | // UpdateArrayViews.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/4. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UpdateArrayViews : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UpdateArrayViews.m: -------------------------------------------------------------------------------- 1 | // 2 | // UpdateArrayViews.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/4. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "UpdateArrayViews.h" 10 | @interface UpdateArrayViews() 11 | 12 | @property (nonatomic, assign) int width; 13 | @property (nonatomic, strong) NSArray *views; 14 | 15 | @end 16 | 17 | 18 | @implementation UpdateArrayViews 19 | 20 | -(instancetype)init { 21 | self = [super init]; 22 | [self addView]; 23 | return self; 24 | } 25 | 26 | - (void)addView { 27 | UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heart"]]; 28 | [imageView1 setContentMode:UIViewContentModeScaleAspectFit]; 29 | [self addSubview:imageView1]; 30 | 31 | UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heart"]]; 32 | [imageView2 setContentMode:UIViewContentModeScaleAspectFit]; 33 | [self addSubview:imageView2]; 34 | 35 | UIImageView *imageView3= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heart"]]; 36 | [imageView3 setContentMode:UIViewContentModeScaleAspectFit]; 37 | [self addSubview:imageView3]; 38 | 39 | self.views = @[imageView1, imageView2, imageView3]; 40 | 41 | 42 | 43 | self.width = 100; 44 | [imageView1 mas_makeConstraints:^(MASConstraintMaker *make) { 45 | make.centerY.equalTo(self).offset(-self.width); 46 | make.centerX.equalTo(self); 47 | make.width.equalTo(@(self.width)); 48 | make.height.equalTo(imageView1.mas_width); 49 | }]; 50 | 51 | 52 | [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) { 53 | make.top.equalTo(imageView1.mas_bottom); 54 | make.left.equalTo(imageView1.mas_left).offset(-self.width); 55 | make.size.equalTo(imageView1); 56 | }]; 57 | 58 | [imageView3 mas_makeConstraints:^(MASConstraintMaker *make) { 59 | make.top.equalTo(imageView1.mas_bottom); 60 | make.right.equalTo(imageView1.mas_right).offset(self.width); 61 | make.size.equalTo(imageView1); 62 | }]; 63 | 64 | 65 | 66 | 67 | 68 | } 69 | 70 | #pragma - mark 视图回调 71 | //视图加载后开始动画 72 | - (void)didMoveToWindow { 73 | [self animateWithInvertedInsets:NO]; 74 | } 75 | 76 | //重写updateConstraints方法 77 | -(void)updateConstraints { 78 | 79 | //更新数组约束 80 | [_views mas_updateConstraints:^(MASConstraintMaker *make) { 81 | make.width.equalTo(@(self.width)); 82 | }]; 83 | 84 | [super updateConstraints]; 85 | } 86 | 87 | 88 | - (void)animateWithInvertedInsets:(BOOL)invertedInsets { 89 | self.width = invertedInsets ? 50 : 200; 90 | 91 | 92 | // 告诉约束需要更新 93 | [self setNeedsUpdateConstraints]; 94 | 95 | // 更新约束 96 | [self updateConstraintsIfNeeded]; 97 | [UIView animateWithDuration:1 animations:^{ 98 | [self layoutIfNeeded]; 99 | } completion:^(BOOL finished) { 100 | //repeat! 101 | [self animateWithInvertedInsets:!invertedInsets]; 102 | }]; 103 | 104 | } 105 | 106 | @end 107 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UpdateConstraintView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UpdateConstraintView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UpdateConstraintView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UpdateConstraintView.m: -------------------------------------------------------------------------------- 1 | // 2 | // UpdateConstraintView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "UpdateConstraintView.h" 10 | 11 | @interface UpdateConstraintView() 12 | @property (nonatomic, strong) UIButton * button; 13 | @property (nonatomic, assign) CGSize buttonSize; 14 | 15 | @end 16 | 17 | @implementation UpdateConstraintView 18 | 19 | -(instancetype)init { 20 | self = [super init]; 21 | self.buttonSize = CGSizeMake(100, 100); 22 | [self addView]; 23 | return self; 24 | } 25 | 26 | - (void)addView { 27 | self.button = [[UIButton alloc] init]; 28 | [self.button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside]; 29 | self.button.backgroundColor = [UIColor redColor]; 30 | self.button.layer.borderWidth = 5; 31 | self.button.layer.borderColor = [[UIColor blackColor] CGColor]; 32 | [self.button setTitle:@"更新约束" forState:UIControlStateNormal]; 33 | [self addSubview:self.button]; 34 | 35 | } 36 | 37 | 38 | //添加这句话就会执行 updateConstraints方法 39 | //约束的Layout方式是lazily使用的。如果你在-updateConstraints中来初始化你的约束, 40 | //但是如果没有添加约束的话,系统就不会调用这个-updateConstraints接口。 41 | //这个就是鸡和蛋的问题,所以用这个方法在自定义的view中返回YES,表示一定要用AL来约束。 42 | + (BOOL)requiresConstraintBasedLayout { 43 | return YES; 44 | } 45 | 46 | //重新该更新约束的方法 47 | -(void)updateConstraints { 48 | [self.button mas_updateConstraints:^(MASConstraintMaker *make) { 49 | make.center.equalTo(self); 50 | make.size.mas_equalTo(self.buttonSize); 51 | }]; 52 | [super updateConstraints]; 53 | } 54 | 55 | - (void)tapButton:(UIButton *)sender { 56 | static int i = 0; 57 | if (i == 0) { 58 | i = 1; 59 | self.buttonSize = CGSizeMake(300, 300); 60 | } else { 61 | i = 0; 62 | self.buttonSize = CGSizeMake(100, 100); 63 | } 64 | 65 | // 告诉约束需要更新 66 | [self setNeedsUpdateConstraints]; 67 | 68 | // 强制更新更新约束(可以不加) 69 | [self updateConstraintsIfNeeded]; 70 | 71 | [UIView animateWithDuration:0.4 animations:^{ 72 | [self layoutIfNeeded]; 73 | }]; 74 | } 75 | @end 76 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UseConstantsView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UseConstantsView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UseConstantsView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UseConstantsView.m: -------------------------------------------------------------------------------- 1 | // 2 | // UseConstantsView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "UseConstantsView.h" 10 | 11 | @implementation UseConstantsView 12 | 13 | -(instancetype)init { 14 | self = [super init]; 15 | [self addView]; 16 | return self; 17 | } 18 | 19 | - (void)addView { 20 | 21 | UIView *redView = [UIView new]; 22 | redView.backgroundColor = [UIColor redColor]; 23 | redView.layer.borderWidth = 2; 24 | redView.layer.borderColor = [[UIColor blackColor] CGColor]; 25 | [self addSubview:redView]; 26 | 27 | 28 | [redView mas_makeConstraints:^(MASConstraintMaker *make) { 29 | 30 | make.top.equalTo(@20); 31 | make.left.equalTo(@20); 32 | make.right.equalTo(@-20); 33 | make.bottom.equalTo(@-20); 34 | 35 | //下方链式调用也是可以的 36 | //make.top.left.right.bottom.equalTo(@20); 37 | }]; 38 | 39 | 40 | 41 | 42 | UIView *greenView = [UIView new]; 43 | greenView.backgroundColor = [UIColor greenColor]; 44 | greenView.layer.borderWidth = 2; 45 | greenView.layer.borderColor = [[UIColor blackColor] CGColor]; 46 | [self addSubview:greenView]; 47 | 48 | 49 | [greenView mas_makeConstraints:^(MASConstraintMaker *make) { 50 | make.center.mas_equalTo(CGPointMake(0, 0)); 51 | make.size.mas_equalTo(CGSizeMake(200, 100)); 52 | }]; 53 | 54 | } 55 | 56 | @end 57 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UseEdgesInsetView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UseEdgesInsetView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UseEdgesInsetView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UseEdgesInsetView.m: -------------------------------------------------------------------------------- 1 | // 2 | // UseEdgesInsetView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/3. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "UseEdgesInsetView.h" 10 | 11 | @implementation UseEdgesInsetView 12 | -(instancetype)init { 13 | self = [super init]; 14 | [self addView]; 15 | return self; 16 | } 17 | 18 | - (void)addView { 19 | UIView *redView = [UIView new]; 20 | redView.backgroundColor = [UIColor redColor]; 21 | redView.layer.borderWidth = 2; 22 | redView.layer.borderColor = [[UIColor blackColor] CGColor]; 23 | [self addSubview:redView]; 24 | 25 | UIView *greenView = [UIView new]; 26 | greenView.backgroundColor = [UIColor greenColor]; 27 | greenView.layer.borderWidth = 2; 28 | greenView.layer.borderColor = [[UIColor blackColor] CGColor]; 29 | [self addSubview:greenView]; 30 | 31 | 32 | [redView mas_makeConstraints:^(MASConstraintMaker *make) { 33 | make.edges.equalTo(self).insets(UIEdgeInsetsMake(50, 50, 50, 50)); 34 | }]; 35 | 36 | 37 | [greenView mas_makeConstraints:^(MASConstraintMaker *make) { 38 | make.edges.equalTo(redView).insets(UIEdgeInsetsMake(50, 50, 50, 50)); 39 | }]; 40 | 41 | } 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UserMarginView.h: -------------------------------------------------------------------------------- 1 | // 2 | // UserMarginView.h 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/4. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UserMarginView : UIView 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/UserMarginView.m: -------------------------------------------------------------------------------- 1 | // 2 | // UserMarginView.m 3 | // MasonryDemo 4 | // 5 | // Created by Mr.LuDashi on 16/5/4. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import "UserMarginView.h" 10 | 11 | @implementation UserMarginView 12 | 13 | - (instancetype)init { 14 | self = [super init]; 15 | if (!self) return nil; 16 | 17 | UIView *lastView = self; 18 | for (int i = 0; i < 20; i++) { 19 | UIView *view = UIView.new; 20 | view.backgroundColor = [self randomColor]; 21 | view.layer.borderColor = UIColor.blackColor.CGColor; 22 | view.layer.borderWidth = 2; 23 | view.layoutMargins = UIEdgeInsetsMake(10, 5, 10, 5); 24 | [self addSubview:view]; 25 | 26 | [view mas_makeConstraints:^(MASConstraintMaker *make) { 27 | make.top.equalTo(lastView.mas_topMargin); 28 | make.bottom.equalTo(lastView.mas_bottomMargin); 29 | make.left.equalTo(lastView.mas_leftMargin); 30 | make.right.equalTo(lastView.mas_rightMargin); 31 | }]; 32 | 33 | lastView = view; 34 | } 35 | 36 | return self; 37 | } 38 | 39 | - (UIColor *)randomColor { 40 | CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0 41 | CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white 42 | CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black 43 | return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; 44 | } 45 | 46 | @end 47 | -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/heart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/MasonryDemo/MasonryDemo/heart.png -------------------------------------------------------------------------------- /MasonryDemo/MasonryDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // MasonryDemo 4 | // 5 | // Created by ZeluLi on 16/4/30. 6 | // Copyright © 2016年 zeluli. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MasonryDemo 2 | ##MasonryDemo和Masonry源码解析 3 | ##类图 4 | ![](http://images2015.cnblogs.com/blog/545446/201605/545446-20160510095722015-1054100603.png) 5 | -------------------------------------------------------------------------------- /类图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lizelu/MasonryDemo/17d12e54034c04c724811d1e0097b075d95de097/类图.png --------------------------------------------------------------------------------