Jonas Budelmann 11 жил өмнө
parent
commit
664be4b654

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

@@ -20,6 +20,7 @@
 		DD52F254179CADC0005CD195 /* MASExampleSidesView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD52F24E179CADC0005CD195 /* MASExampleSidesView.m */; };
 		DD52F255179CADC0005CD195 /* MASExampleViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DD52F250179CADC0005CD195 /* MASExampleViewController.m */; };
 		DD7CC17617ACE990007A469E /* MASExampleDebuggingView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD7CC17517ACE990007A469E /* MASExampleDebuggingView.m */; };
+		DD9B4D35183CC980002BF408 /* MASExampleScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = DD9B4D34183CC980002BF408 /* MASExampleScrollView.m */; };
 		DDA5755F17C1B07E0010F88E /* MASCompositeConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = DDA5754D17C1B07E0010F88E /* MASCompositeConstraint.m */; };
 		DDA5756017C1B07E0010F88E /* MASConstraintMaker.m in Sources */ = {isa = PBXBuildFile; fileRef = DDA5755017C1B07E0010F88E /* MASConstraintMaker.m */; };
 		DDA5756117C1B07E0010F88E /* MASLayoutConstraint.m in Sources */ = {isa = PBXBuildFile; fileRef = DDA5755217C1B07E0010F88E /* MASLayoutConstraint.m */; };
@@ -56,6 +57,8 @@
 		DD52F250179CADC0005CD195 /* MASExampleViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleViewController.m; sourceTree = "<group>"; };
 		DD7CC17417ACE990007A469E /* MASExampleDebuggingView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleDebuggingView.h; sourceTree = "<group>"; };
 		DD7CC17517ACE990007A469E /* MASExampleDebuggingView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleDebuggingView.m; sourceTree = "<group>"; };
+		DD9B4D33183CC980002BF408 /* MASExampleScrollView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASExampleScrollView.h; sourceTree = "<group>"; };
+		DD9B4D34183CC980002BF408 /* MASExampleScrollView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASExampleScrollView.m; sourceTree = "<group>"; };
 		DDA5754C17C1B07E0010F88E /* MASCompositeConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASCompositeConstraint.h; sourceTree = "<group>"; };
 		DDA5754D17C1B07E0010F88E /* MASCompositeConstraint.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MASCompositeConstraint.m; sourceTree = "<group>"; };
 		DDA5754E17C1B07E0010F88E /* MASConstraint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MASConstraint.h; sourceTree = "<group>"; };
@@ -162,6 +165,8 @@
 				DDDF60CB181915E300BF7B8B /* MASExampleLabelView.m */,
 				DD175E68182639FB0099129A /* MASExampleUpdateView.h */,
 				DD175E69182639FB0099129A /* MASExampleUpdateView.m */,
+				DD9B4D33183CC980002BF408 /* MASExampleScrollView.h */,
+				DD9B4D34183CC980002BF408 /* MASExampleScrollView.m */,
 			);
 			name = Views;
 			sourceTree = "<group>";
@@ -284,6 +289,7 @@
 				DDA5756317C1B07E0010F88E /* MASViewConstraint.m in Sources */,
 				DDA5756417C1B07E0010F88E /* NSLayoutConstraint+MASDebugAdditions.m in Sources */,
 				DDA5756517C1B07E0010F88E /* View+MASAdditions.m in Sources */,
+				DD9B4D35183CC980002BF408 /* MASExampleScrollView.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

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

@@ -15,6 +15,7 @@
 #import "MASExampleDebuggingView.h"
 #import "MASExampleLabelView.h"
 #import "MASExampleUpdateView.h"
+#import "MASExampleScrollView.h"
 
 static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
 
@@ -47,6 +48,8 @@ static NSString * const kMASCellReuseIdentifier = @"kMASCellReuseIdentifier";
                                               viewClass:MASExampleDebuggingView.class],
         [[MASExampleViewController alloc] initWithTitle:@"Bacony Labels"
                                               viewClass:MASExampleLabelView.class],
+        [[MASExampleViewController alloc] initWithTitle:@"UIScrollView"
+                                              viewClass:MASExampleScrollView.class],
     ];
     
     return self;

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

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

+ 72 - 0
MasonryExamples/Masonry iOS Examples/MASExampleScrollView.m

@@ -0,0 +1,72 @@
+//
+//  MASExampleScrollView.m
+//  Masonry iOS Examples
+//
+//  Created by Jonas Budelmann on 20/11/13.
+//  Copyright (c) 2013 Jonas Budelmann. All rights reserved.
+//
+
+#import "MASExampleScrollView.h"
+
+/**
+ *  UIScrollView and Auto Layout don't play very nicely together see
+ *  https://developer.apple.com/library/ios/technotes/tn2154/_index.html
+ *
+ *  This is an example of one workaround
+ *
+ *  for another approach see https://github.com/bizz84/MVScrollViewAutoLayout
+ */
+
+@implementation MASExampleScrollView
+
+- (id)init {
+    self = [super init];
+    if (!self) return nil;
+
+    UIScrollView *scrollView = UIScrollView.new;
+    scrollView.backgroundColor = [UIColor grayColor];
+    [self addSubview:scrollView];
+
+    [scrollView makeConstraints:^(MASConstraintMaker *make) {
+        make.edges.equalTo(self);
+    }];
+
+    UIView *lastView;
+    CGFloat height = 20;
+
+    for (int i = 0; i < 10; i++) {
+        UIView *view = UIView.new;
+        view.backgroundColor = [self randomColor];
+        [scrollView addSubview:view];
+
+        [view mas_makeConstraints:^(MASConstraintMaker *make) {
+            make.top.equalTo(lastView ? lastView.bottom : @0);
+            make.left.equalTo(@0);
+            make.width.equalTo(scrollView.width);
+            make.height.equalTo(@(height));
+        }];
+
+        height += 20;
+        lastView = view;
+    }
+
+    // dummy view, which determines the contentSize of scroll view
+    UIView *sizingView = UIView.new;
+    [scrollView addSubview:sizingView];
+
+    [sizingView makeConstraints:^(MASConstraintMaker *make) {
+        make.top.equalTo(lastView.bottom);
+        make.bottom.equalTo(scrollView.bottom);
+    }];
+
+    return self;
+}
+
+- (UIColor *)randomColor {
+    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
+    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
+    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
+    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
+}
+
+@end