I think I'm trying to do the same thing your doing. I wanted to get the specific overlap between two rects. I found CGRectUnion to be super helpful as it gives the smallest rect containing the two rects. If the size is larger than your "max" then you know the rect is overlapping to the right or bottom. If the origin is negative then you know the rect is overlapping to the top or left. I wrote this function to give the resulting offset needed to correct the overlap.
CGPoint GetOffsetBetweenFrames(CGRect maxBounds, CGRect frame)
{
CGRect frameUnion = CGRectUnion(maxBounds, frame);
CGPoint offset = CGPointZero;
if (CGRectGetMinX(frameUnion) < 0)
offset.x = -frameUnion.origin.x;
else if (CGRectGetWidth(frameUnion) > CGRectGetWidth(maxBounds))
offset.x = -(CGRectGetWidth(frameUnion) - CGRectGetWidth(maxBounds));
if (CGRectGetMinY(frameUnion) < 0)
offset.y = -frameUnion.origin.y;
else if (CGRectGetHeight(frameUnion) > CGRectGetHeight(maxBounds))
offset.y = -(CGRectGetHeight(frameUnion) - CGRectGetHeight(maxBounds));
return offset;
}
Oh, also quick edit: To apply the offset its as simple as:
offset = GetOffsetBetweenFrames(maxBounds, frame);
frame = CGRectOffset(frame, offset.x, offset.y);