Skip to content
Browse files

added -(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribut…

…e toSameAttributeOfItem:(id)peerItem
  • Loading branch information...
1 parent 5c27837 commit b6d8fb6893deb84caa7103ec009354fa18fb5063 @kgn kgn committed Dec 8, 2013
Showing with 26 additions and 3 deletions.
  1. +4 −1 Source/UIView+AutoLayout.h
  2. +22 −2 Source/UIView+AutoLayout.m
View
5 Source/UIView+AutoLayout.h
@@ -23,7 +23,10 @@ typedef NS_OPTIONS(unsigned long, JRTViewPinEdges){
-(NSLayoutConstraint *)centerInContainerOnAxis:(NSLayoutAttribute)axis;
// Pin an attribute to the same attribute on another view. Both views must be in the same view hierarchy
--(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView;
+-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView __deprecated;
+
+/// Pins an attribute to the same attribute of the peer item. The item may be the layout guide of a view controller
+-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfItem:(id)peerItem;
/// Pins a view to a specific edge(s) of its superview, with a specified inset
-(NSArray*)pinToSuperviewEdges:(JRTViewPinEdges)edges inset:(CGFloat)inset;
View
24 Source/UIView+AutoLayout.m
@@ -39,8 +39,7 @@ -(NSLayoutConstraint *)centerInContainerOnAxis:(NSLayoutAttribute)axis
return constraint;
}
-
--(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView;
+-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfView:(UIView *)peerView
{
NSParameterAssert(peerView);
UIView *superview = [self commonSuperviewWithView:peerView];
@@ -51,6 +50,27 @@ -(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttribute
return constraint;
}
+
+-(NSLayoutConstraint *)pinAttribute:(NSLayoutAttribute)attribute toSameAttributeOfItem:(id)peerItem
+{
+ NSParameterAssert(peerItem);
+ UIView *superview;
+ if ([peerItem isKindOfClass:[UIView class]])
+ {
+ superview = [self commonSuperviewWithView:peerItem];
+ NSAssert(superview,@"Can't create constraints without a common superview");
+ }
+ else
+ {
+ superview = self.superview;
+ }
+ NSAssert(superview,@"Can't create constraints without a common superview");
+
+ NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:peerItem attribute:attribute multiplier:1.0 constant:0.0];
+ [superview addConstraint:constraint];
+ return constraint;
+}
+
-(NSArray*)pinToSuperviewEdges:(JRTViewPinEdges)edges inset:(CGFloat)inset
{
return [self pinToSuperviewEdges:edges inset:inset usingLayoutGuidesFrom:nil];

0 comments on commit b6d8fb6

Please sign in to comment.
Something went wrong with that request. Please try again.