Skip to content

Commit

Permalink
Add calls for alignRelativeToView and alignRelativeToSuperView that t…
Browse files Browse the repository at this point in the history
…ake masks of alignment options as well as edge insets for single call alignment.
  • Loading branch information
eriklamanna committed Nov 7, 2012
1 parent 8b37213 commit ce9587a
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 5 deletions.
30 changes: 30 additions & 0 deletions UIView+Helpers.h
Expand Up @@ -7,10 +7,40 @@

@interface UIView (Helpers)

enum {
UIViewAlignmentLeft = 1 << 0,
UIViewAlignmentRight = 1 << 1,
UIViewAlignmentTop = 1 << 2,
UIViewAlignmentBottom = 1 << 3,

UIViewAlignmentLeftEdge = 1 << 5,
UIViewAlignmentRightEdge = 1 << 6,
UIViewAlignmentTopEdge = 1 << 7,
UIViewAlignmentBottomEdge = 1 << 8,

UIViewAlignmentHorizontalCenter = 1 << 9,
UIViewAlignmentVerticalCenter = 1 << 10,
};
typedef NSUInteger UIViewAlignment;

+ (CGRect) alignRect:(CGRect)startingRect
toRect:(CGRect)referenceRect
withAlignment:(UIViewAlignment)alignment
insets:(UIEdgeInsets)insets
andReferenceIsSuperView:(BOOL)isReferenceSuperView;

// Init
- (id)initWithSize:(CGSize)size;

//Alignment
- (void)alignRelativeToView:(UIView*)alignView
withAlignment:(UIViewAlignment)alignment
andInsets:(UIEdgeInsets)insets;

- (void)alignRelativeToSuperView:(UIView*)alignView
withAlignment:(UIViewAlignment)alignment
andInsets:(UIEdgeInsets)insets;

- (void)centerAlignHorizontalForView:(UIView *)view;
- (void)centerAlignVerticalForView:(UIView *)view;

Expand Down
113 changes: 108 additions & 5 deletions UIView+Helpers.m
Expand Up @@ -24,6 +24,114 @@ - (id)initWithSize:(CGSize)size
return self;
}

+ (CGRect) alignRect:(CGRect)startingRect toRect:(CGRect)referenceRect withAlignment:(UIViewAlignment)alignment insets:(UIEdgeInsets)insets andReferenceIsSuperView:(BOOL)isReferenceSuperView
{
CGRect newRect = startingRect;

// X alignments
if (alignment & UIViewAlignmentLeft)
{
newRect.origin.x += CGRectGetMinX(referenceRect) - (CGRectGetWidth(startingRect) + insets.right);
}
else if (alignment & UIViewAlignmentRight)
{
newRect.origin.x = CGRectGetMaxX(referenceRect) + insets.left;
}
else if (alignment & UIViewAlignmentLeftEdge)
{
if (isReferenceSuperView)
{
newRect.origin.x = insets.left;
}
else
{
newRect.origin.x = referenceRect.origin.x + insets.left;
}
}
else if (alignment & UIViewAlignmentRightEdge)
{
if (isReferenceSuperView)
{
newRect.origin.x = CGRectGetWidth(referenceRect) - (CGRectGetWidth(startingRect) + insets.right);
}
else
{
newRect.origin.x = CGRectGetMaxX(referenceRect) - (CGRectGetWidth(startingRect) + insets.right);
}
}
else if (alignment & UIViewAlignmentHorizontalCenter)
{
if (isReferenceSuperView)
{
newRect.origin.x = (((CGRectGetWidth(referenceRect) - CGRectGetWidth(startingRect))) / 2.0f);
}
else
{
newRect.origin.x = CGRectGetMinX(referenceRect) +
(((CGRectGetWidth(referenceRect) - CGRectGetWidth(startingRect))) / 2.0f);
}
}

// Y alignments
if (alignment & UIViewAlignmentTop)
{
newRect.origin.y = CGRectGetMinY(referenceRect) - (CGRectGetHeight(startingRect) + insets.bottom);
}
else if (alignment & UIViewAlignmentBottom)
{
newRect.origin.y = CGRectGetMaxY(referenceRect) + insets.top;
}
else if (alignment & UIViewAlignmentBottomEdge)
{
if (isReferenceSuperView)
{
newRect.origin.y = CGRectGetHeight(referenceRect) - (CGRectGetHeight(startingRect) + insets.bottom);
}
else
{
newRect.origin.y = CGRectGetMaxY(referenceRect) - (CGRectGetHeight(startingRect) + insets.bottom);
}
}
else if (alignment & UIViewAlignmentTopEdge)
{
if (isReferenceSuperView)
{
newRect.origin.y = insets.top;
}
else
{
newRect.origin.y = CGRectGetMinY(referenceRect) + insets.top;
}
}
else if (alignment & UIViewAlignmentVerticalCenter)
{
if (isReferenceSuperView)
{
newRect.origin.y = ((CGRectGetHeight(referenceRect) - CGRectGetHeight(startingRect)) / 2.0f) + insets.top - insets.bottom;
}
else
{

newRect.origin.y = CGRectGetMinY(referenceRect) +
((CGRectGetHeight(referenceRect) - CGRectGetHeight(startingRect)) / 2.0f) + insets.top - insets.bottom;
}
}

return CGRectIntegral(newRect);

}

- (void)alignRelativeToView:(UIView*)alignView withAlignment:(UIViewAlignment)alignment andInsets:(UIEdgeInsets)insets
{
self.frame = [UIView alignRect:self.frame toRect:alignView.frame withAlignment:alignment insets:insets andReferenceIsSuperView:NO];
}

- (void)alignRelativeToSuperView:(UIView*)alignView withAlignment:(UIViewAlignment)alignment andInsets:(UIEdgeInsets)insets
{
self.frame = [UIView alignRect:self.frame toRect:alignView.frame withAlignment:alignment insets:insets andReferenceIsSuperView:YES];
}


#pragma mark -
#pragma mark Alignment

Expand All @@ -47,11 +155,6 @@ - (void)centerAlignVerticalForSuperView
[self centerAlignVerticalForView:self.superview];
}

- (void)centerAlignForSuperviewOffset:(CGPoint)offset
{
[self centerAlignForView:self.superview offset:offset];
}

- (void)centerAlignHorizontalForSuperView:(CGFloat)offset
{
[self centerAlignHorizontalForView:self.superview offset:offset];
Expand Down

0 comments on commit ce9587a

Please sign in to comment.