Jelajahi Sumber

Added support for adding a constraint between a nested view and a layout guide. Added seperate view and item to MASViewAttribute.

Craig Siemens 10 tahun lalu
induk
melakukan
85a5e18f22

+ 13 - 0
Examples/Masonry iOS Examples/MASExampleLayoutGuideViewController.m

@@ -34,6 +34,12 @@
     topView.layer.borderWidth = 2;
     [self.view addSubview:topView];
 
+    UIView *topSubview = UIView.new;
+    topSubview.backgroundColor = UIColor.blueColor;
+    topSubview.layer.borderColor = UIColor.blackColor.CGColor;
+    topSubview.layer.borderWidth = 2;
+    [topView addSubview:topSubview];
+    
     UIView *bottomView = UIView.new;
     bottomView.backgroundColor = UIColor.redColor;
     bottomView.layer.borderColor = UIColor.blackColor.CGColor;
@@ -46,6 +52,13 @@
         make.right.equalTo(self.view);
         make.height.equalTo(@40);
     }];
+
+    [topSubview makeConstraints:^(MASConstraintMaker *make) {
+        make.top.equalTo(self.mas_topLayoutGuide);
+        make.centerX.equalTo(@0);
+        make.width.equalTo(@20);
+        make.height.equalTo(@20);
+    }];
     
     [bottomView makeConstraints:^(MASConstraintMaker *make) {
         make.bottom.equalTo(self.mas_bottomLayoutGuide);

+ 12 - 2
Masonry/MASViewAttribute.h

@@ -15,20 +15,30 @@
 @interface MASViewAttribute : NSObject
 
 /**
- *  The view which the reciever relates to
+ *  The view which the reciever relates to. Can be nil if item is not a view.
  */
 @property (nonatomic, weak, readonly) MAS_VIEW *view;
 
+/**
+ *  The item which the reciever relates to.
+ */
+@property (nonatomic, weak, readonly) id item;
+
 /**
  *  The attribute which the reciever relates to
  */
 @property (nonatomic, assign, readonly) NSLayoutAttribute layoutAttribute;
 
 /**
- *  The designated initializer.
+ *  Convenience initializer.
  */
 - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute;
 
+/**
+ *  The designated initializer.
+ */
+- (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute;
+
 /**
  *	Determine whether the layoutAttribute is a size attribute
  *

+ 6 - 0
Masonry/MASViewAttribute.m

@@ -11,10 +11,16 @@
 @implementation MASViewAttribute
 
 - (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute {
+    self = [self initWithView:view item:view layoutAttribute:layoutAttribute];
+    return self;
+}
+
+- (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute {
     self = [super init];
     if (!self) return nil;
     
     _view = view;
+    _item = item;
     _layoutAttribute = layoutAttribute;
     
     return self;

+ 9 - 8
Masonry/MASViewConstraint.m

@@ -313,17 +313,16 @@ static char kInstalledConstraintsKey;
         return;
     }
     
-    MAS_VIEW *firstLayoutItem = self.firstViewAttribute.view;
-    
+    MAS_VIEW *firstLayoutItem = self.firstViewAttribute.item;
     NSLayoutAttribute firstLayoutAttribute = self.firstViewAttribute.layoutAttribute;
-    MAS_VIEW *secondLayoutItem = self.secondViewAttribute.view;
+    MAS_VIEW *secondLayoutItem = self.secondViewAttribute.item;
     NSLayoutAttribute secondLayoutAttribute = self.secondViewAttribute.layoutAttribute;
 
     // alignment attributes must have a secondViewAttribute
     // therefore we assume that is refering to superview
     // eg make.left.equalTo(@10)
     if (!self.firstViewAttribute.isSizeAttribute && !self.secondViewAttribute) {
-        secondLayoutItem = firstLayoutItem.superview;
+        secondLayoutItem = self.firstViewAttribute.view.superview;
         secondLayoutAttribute = firstLayoutAttribute;
     }
     
@@ -339,14 +338,16 @@ static char kInstalledConstraintsKey;
     layoutConstraint.priority = self.layoutPriority;
     layoutConstraint.mas_key = self.mas_key;
     
-    if (secondLayoutItem) {
-        MAS_VIEW *closestCommonSuperview = [firstLayoutItem mas_closestCommonSuperview:secondLayoutItem];
+    if (self.secondViewAttribute.view) {
+        MAS_VIEW *closestCommonSuperview = [self.firstViewAttribute.view mas_closestCommonSuperview:self.secondViewAttribute.view];
         NSAssert(closestCommonSuperview,
                  @"couldn't find a common superview for %@ and %@",
-                 firstLayoutItem, secondLayoutItem);
+                 self.firstViewAttribute.view, self.secondViewAttribute.view);
         self.installedView = closestCommonSuperview;
+    } else if (self.firstViewAttribute.isSizeAttribute) {
+        self.installedView = self.firstViewAttribute.view;
     } else {
-        self.installedView = firstLayoutItem;
+        self.installedView = self.firstViewAttribute.view.superview;
     }
 
 

+ 2 - 2
Masonry/ViewController+MASAdditions.m

@@ -13,11 +13,11 @@
 @implementation MAS_VIEW_CONTROLLER (MASAdditions)
 
 - (MASViewAttribute *)mas_topLayoutGuide {
-    return [[MASViewAttribute alloc] initWithView:(UIView *)self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom];
+    return [[MASViewAttribute alloc] initWithView:self.view item:self.topLayoutGuide layoutAttribute:NSLayoutAttributeBottom];
 }
 
 - (MASViewAttribute *)mas_bottomLayoutGuide {
-    return [[MASViewAttribute alloc] initWithView:(UIView *)self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop];
+    return [[MASViewAttribute alloc] initWithView:self.view item:self.bottomLayoutGuide layoutAttribute:NSLayoutAttributeTop];
 }
 
 @end

TEMPAT SAMPAH
Pods/ObjectiveC.gcda


TEMPAT SAMPAH
Pods/ObjectiveC.gcno