Forráskód Böngészése

adding mas_updateConstraints example

Jonas Budelmann 11 éve
szülő
commit
19a7e72103

+ 1 - 1
Masonry/View+MASAdditions.m

@@ -21,8 +21,8 @@
 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block {
 - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *))block {
     self.translatesAutoresizingMaskIntoConstraints = NO;
     self.translatesAutoresizingMaskIntoConstraints = NO;
     MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
     MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
-    block(constraintMaker);
     constraintMaker.updateExisting = YES;
     constraintMaker.updateExisting = YES;
+    block(constraintMaker);
     return [constraintMaker install];
     return [constraintMaker install];
 }
 }
 
 

+ 6 - 0
MasonryExamples/Masonry iOS Examples.xcodeproj/project.pbxproj

@@ -7,6 +7,7 @@
 	objects = {
 	objects = {
 
 
 /* Begin PBXBuildFile section */
 /* Begin PBXBuildFile section */
+		DD175E6A182639FB0099129A /* MASExampleUpdateView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD175E69182639FB0099129A /* MASExampleUpdateView.m */; };
 		DD52F22B179CAD57005CD195 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F22A179CAD57005CD195 /* UIKit.framework */; };
 		DD52F22B179CAD57005CD195 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F22A179CAD57005CD195 /* UIKit.framework */; };
 		DD52F22D179CAD57005CD195 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F22C179CAD57005CD195 /* Foundation.framework */; };
 		DD52F22D179CAD57005CD195 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F22C179CAD57005CD195 /* Foundation.framework */; };
 		DD52F22F179CAD57005CD195 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F22E179CAD57005CD195 /* CoreGraphics.framework */; };
 		DD52F22F179CAD57005CD195 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD52F22E179CAD57005CD195 /* CoreGraphics.framework */; };
@@ -31,6 +32,8 @@
 /* End PBXBuildFile section */
 /* End PBXBuildFile section */
 
 
 /* Begin PBXFileReference section */
 /* Begin PBXFileReference section */
+		DD175E68182639FB0099129A /* MASExampleUpdateView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleUpdateView.h; sourceTree = "<group>"; };
+		DD175E69182639FB0099129A /* MASExampleUpdateView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleUpdateView.m; sourceTree = "<group>"; };
 		DD52F227179CAD57005CD195 /* Masonry iOS Examples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Masonry iOS Examples.app"; sourceTree = BUILT_PRODUCTS_DIR; };
 		DD52F227179CAD57005CD195 /* Masonry iOS Examples.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Masonry iOS Examples.app"; sourceTree = BUILT_PRODUCTS_DIR; };
 		DD52F22A179CAD57005CD195 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
 		DD52F22A179CAD57005CD195 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
 		DD52F22C179CAD57005CD195 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
 		DD52F22C179CAD57005CD195 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -157,6 +160,8 @@
 				DD7CC17517ACE990007A469E /* MASExampleDebuggingView.m */,
 				DD7CC17517ACE990007A469E /* MASExampleDebuggingView.m */,
 				DDDF60CA181915E300BF7B8B /* MASExampleLabelView.h */,
 				DDDF60CA181915E300BF7B8B /* MASExampleLabelView.h */,
 				DDDF60CB181915E300BF7B8B /* MASExampleLabelView.m */,
 				DDDF60CB181915E300BF7B8B /* MASExampleLabelView.m */,
+				DD175E68182639FB0099129A /* MASExampleUpdateView.h */,
+				DD175E69182639FB0099129A /* MASExampleUpdateView.m */,
 			);
 			);
 			name = Views;
 			name = Views;
 			sourceTree = "<group>";
 			sourceTree = "<group>";
@@ -261,6 +266,7 @@
 			isa = PBXSourcesBuildPhase;
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			buildActionMask = 2147483647;
 			files = (
 			files = (
+				DD175E6A182639FB0099129A /* MASExampleUpdateView.m in Sources */,
 				DD52F237179CAD57005CD195 /* main.m in Sources */,
 				DD52F237179CAD57005CD195 /* main.m in Sources */,
 				DD52F23B179CAD57005CD195 /* MASAppDelegate.m in Sources */,
 				DD52F23B179CAD57005CD195 /* MASAppDelegate.m in Sources */,
 				DD52F251179CADC0005CD195 /* MASExampleBasicView.m in Sources */,
 				DD52F251179CADC0005CD195 /* MASExampleBasicView.m in Sources */,

+ 3 - 0
MasonryExamples/Masonry iOS Examples/MASExampleListViewController.m

@@ -14,6 +14,7 @@
 #import "MASExampleAnimatedView.h"
 #import "MASExampleAnimatedView.h"
 #import "MASExampleDebuggingView.h"
 #import "MASExampleDebuggingView.h"
 #import "MASExampleLabelView.h"
 #import "MASExampleLabelView.h"
+#import "MASExampleUpdateView.h"
 
 
 static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
 static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
 
 
@@ -44,6 +45,8 @@ static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
                                               viewClass:MASExampleDebuggingView.class],
                                               viewClass:MASExampleDebuggingView.class],
         [[MASExampleViewController alloc] initWithTitle:@"Bacony Labels"
         [[MASExampleViewController alloc] initWithTitle:@"Bacony Labels"
                                               viewClass:MASExampleLabelView.class],
                                               viewClass:MASExampleLabelView.class],
+        [[MASExampleViewController alloc] initWithTitle:@"Update constraints"
+                                              viewClass:MASExampleUpdateView.class],
     ];
     ];
     
     
     return self;
     return self;

+ 13 - 0
MasonryExamples/Masonry iOS Examples/MASExampleUpdateView.h

@@ -0,0 +1,13 @@
+//
+//  MASExampleUpdateView.h
+//  Masonry iOS Examples
+//
+//  Created by Jonas Budelmann on 3/11/13.
+//  Copyright (c) 2013 Jonas Budelmann. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface MASExampleUpdateView : UIView
+
+@end

+ 59 - 0
MasonryExamples/Masonry iOS Examples/MASExampleUpdateView.m

@@ -0,0 +1,59 @@
+//
+//  MASExampleUpdateView.m
+//  Masonry iOS Examples
+//
+//  Created by Jonas Budelmann on 3/11/13.
+//  Copyright (c) 2013 Jonas Budelmann. All rights reserved.
+//
+
+#import "MASExampleUpdateView.h"
+
+@interface MASExampleUpdateView ()
+
+@property (nonatomic, strong) UIButton *growingButton;
+@property (nonatomic, assign) CGSize buttonSize;
+
+@end
+
+@implementation MASExampleUpdateView
+
+- (id)init {
+    self = [super init];
+    if (!self) return nil;
+
+    self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
+    [self.growingButton setTitle:@"Grow Me!" forState:UIControlStateNormal];
+    self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
+    self.growingButton.layer.borderWidth = 2;
+
+    [self.growingButton addTarget:self action:@selector(didTapGrowButton:) forControlEvents:UIControlEventTouchUpInside];
+    [self addSubview:self.growingButton];
+
+    self.buttonSize = CGSizeMake(100, 100);
+    [self setNeedsUpdateConstraints];
+
+    return self;
+}
+
+- (void)updateConstraints {
+    [super updateConstraints];
+
+    [self.growingButton updateConstraints:^(MASConstraintMaker *make) {
+        make.center.equalTo(self);
+        make.width.equalTo(@(self.buttonSize.width));
+        make.height.equalTo(@(self.buttonSize.height));
+    }];
+}
+
+- (void)didTapGrowButton:(UIButton *)button {
+    self.buttonSize = CGSizeMake(self.buttonSize.width * 1.1, self.buttonSize.height * 1.1);
+
+    [self setNeedsUpdateConstraints];
+    [self updateConstraintsIfNeeded];
+
+    [UIView animateWithDuration:0.4 animations:^{
+        [self layoutIfNeeded];
+    }];
+}
+
+@end

+ 4 - 0
MasonryTests/MASViewAttributeSpec.m

@@ -45,6 +45,10 @@ context(@"isEqual", ^{
                                                                       layoutAttribute:NSLayoutAttributeRight];
                                                                       layoutAttribute:NSLayoutAttributeRight];
         expect([viewAttribute isEqual:otherViewAttribute]).to.equal(NO);
         expect([viewAttribute isEqual:otherViewAttribute]).to.equal(NO);
     });
     });
+
+    it(@"should return NO when non view attribute passed", ^{
+        expect([viewAttribute isEqual:NSArray.new]).to.equal(NO);
+    });
 });
 });
 
 
 context(@"hash", ^{
 context(@"hash", ^{

+ 2 - 2
MasonryTests/NSLayoutConstraint+MASDebugAdditionsSpec.m

@@ -32,10 +32,10 @@ it(@"should display layoutConstraint key", ^{
     MAS_VIEW *newView2 = MAS_VIEW.new;
     MAS_VIEW *newView2 = MAS_VIEW.new;
     newView2.mas_key = @"newView2";
     newView2.mas_key = @"newView2";
 
 
-    MASLayoutConstraint *layoutConstraint = [MASLayoutConstraint constraintWithItem:newView1 attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:newView2 attribute:NSLayoutAttributeTop multiplier:1 constant:300];
+    MASLayoutConstraint *layoutConstraint = [MASLayoutConstraint constraintWithItem:newView1 attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:newView2 attribute:NSLayoutAttributeTop multiplier:2 constant:300];
     layoutConstraint.mas_key = @"helloConstraint";
     layoutConstraint.mas_key = @"helloConstraint";
 
 
-    NSString *description = [NSString stringWithFormat:@"<MASLayoutConstraint:helloConstraint %@:newView1.baseline == %@:newView2.top + 300>", MAS_VIEW.class, MAS_VIEW.class];
+    NSString *description = [NSString stringWithFormat:@"<MASLayoutConstraint:helloConstraint %@:newView1.baseline == %@:newView2.top * 2 + 300>", MAS_VIEW.class, MAS_VIEW.class];
     expect([layoutConstraint description]).to.equal(description);
     expect([layoutConstraint description]).to.equal(description);
 });
 });
 
 

+ 9 - 0
MasonryTests/View+MASAdditionsSpec.m

@@ -13,7 +13,16 @@ SpecBegin(View_MASAdditions)
 it(@"should set translatesAutoresizingMaskIntoConstraints", ^{
 it(@"should set translatesAutoresizingMaskIntoConstraints", ^{
     MAS_VIEW *newView = MAS_VIEW.new;
     MAS_VIEW *newView = MAS_VIEW.new;
     [newView mas_makeConstraints:^(MASConstraintMaker *make) {
     [newView mas_makeConstraints:^(MASConstraintMaker *make) {
+        expect(make.updateExisting).to.beFalsy();
+    }];
 
 
+    expect(newView.translatesAutoresizingMaskIntoConstraints).to.beFalsy();
+});
+
+it(@"should set updateExisting", ^{
+    MAS_VIEW *newView = MAS_VIEW.new;
+    [newView mas_updateConstraints:^(MASConstraintMaker *make) {
+        expect(make.updateExisting).to.beTruthy();
     }];
     }];
 
 
     expect(newView.translatesAutoresizingMaskIntoConstraints).to.beFalsy();
     expect(newView.translatesAutoresizingMaskIntoConstraints).to.beFalsy();