| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749 |
- //
- // UIView+CustomAutoLayout.m
- // CommonLibrary
- //
- // Created by Alexi on 13-11-20.
- // Copyright (c) 2013年 ywchen. All rights reserved.
- //
- #import "UIView+CustomAutoLayout.h"
- @implementation UIView (CustomAutoLayout)
- - (UIView *)sameWith:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- self.frame = brotherView.frame;
- return self;
- }
- // 同一控件内两子之空间之间的相对位置
- // margin 之间的空隙
- // brotherView.superView与self.superView相同
- // brotherView必须要先设置了frame
- //- (BOOL)isArgValid:(UIView *)brotherView
- //{
- // return (self.superview == brotherView.superview) && (CGRectEqualToRect(brotherView.frame, CGRectZero));
- //}
- // 会影响origin的位置,会影响其高度
- - (UIView *)layoutAbove:(UIView *)brotherView
- {
- return [self layoutAbove:brotherView margin:0];
- }
- - (UIView *)layoutAbove:(UIView *)brotherView margin:(CGFloat)margin
- {
- // BOOL vailed = (self.superview == brotherView.superview) && !(CGRectEqualToRect(brotherView.frame, CGRectZero));
- // NSAssert(vailed, @"UIView (CustomAutoLayout)参数出错");
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.y = brect.origin.y - margin - rect.size.height;
- self.frame = rect;
- return self;
- }
- - (UIView *)layoutBelow:(UIView *)brotherView
- {
- return [self layoutBelow:brotherView margin:0];
- }
- - (UIView *)layoutBelow:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.y = brect.origin.y + brect.size.height + margin;
- self.frame = rect;
- return self;
- }
- // 会影响origin的位置,会影响其宽度
- - (UIView *)layoutToLeftOf:(UIView *)brotherView
- {
- return [self layoutToLeftOf:brotherView margin:0];
- }
- - (UIView *)layoutToLeftOf:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.x = brect.origin.x - margin - rect.size.width;
- self.frame = rect;
- return self;
- }
- - (UIView *)layoutToRightOf:(UIView *)brotherView
- {
- return [self layoutToRightOf:brotherView margin:0];
- }
- - (UIView *)layoutToRightOf:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.x = brect.origin.x + brect.size.width + margin;
- self.frame = rect;
- return self;
- }
- ///填充式布局
- - (UIView *)scaleToAboveOf:(UIView *)brotherView
- {
- return [self scaleToAboveOf:brotherView margin:0];
- }
- - (UIView *)scaleToAboveOf:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
-
- if (brect.origin.y > rect.origin.y)
- {
- rect.size.height = brect.origin.y - margin - rect.origin.y;
- }
- else
- {
- rect.size.height = rect.origin.y + rect.size.height - (brect.origin.y + margin);
- rect.origin.y = brect.origin.y + margin;
- }
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleToBelowOf:(UIView *)brotherView
- {
- return [self scaleToBelowOf:brotherView margin:0];
- }
- - (UIView *)scaleToBelowOf:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
-
- if (rect.origin.y < brect.origin.y + brect.size.height) {
- rect.size.height = brect.origin.y + brect.size.height - margin - rect.origin.y;
- }
- else
- {
- rect.size.height = rect.origin.y + rect.size.height - (margin + brect.origin.y + brect.size.height);
- rect.origin.y = (margin + brect.origin.y + brect.size.height);
- }
-
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleToLeftOf:(UIView *)brotherView
- {
- return [self scaleToLeftOf:brotherView margin:0];
- }
- - (UIView *)scaleToLeftOf:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
-
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
-
- if (rect.origin.x < brect.origin.x)
- {
- rect.size.width = brect.origin.x - margin - rect.origin.x;
- }
- else
- {
- rect.size.width = rect.origin.x + rect.size.width - (margin + brect.origin.x);
- rect.origin.x = brect.origin.x + margin;
- }
-
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleToRightOf:(UIView *)brotherView
- {
- return [self scaleToRightOf:brotherView margin:0];
- }
- - (UIView *)scaleToRightOf:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
-
- if (rect.origin.x < brect.origin.x + brect.size.width)
- {
- rect.size.width = brect.origin.x + brect.size.width - margin - rect.origin.x;
- }
- else
- {
- rect.size.width = rect.origin.x + rect.size.width - (brect.origin.x + brect.size.width - margin);
- rect.origin.x = brect.origin.x + brect.size.width - margin;
- }
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleParent
- {
- self.frame = self.superview.bounds;
- return self;
- }
- - (UIView *)scaleToParentTop
- {
- return [self scaleToParentTopWithMargin:0];
- }
- - (UIView *)scaleToParentTopWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect sRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.size.height = rect.origin.y + rect.size.height - (sRect.origin.y + margin);
- rect.origin.y = sRect.origin.y + margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleToParentBottom
- {
- return [self scaleToParentBottomWithMargin:0];
- }
- - (UIView *)scaleToParentBottomWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect sRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.size.height = sRect.origin.y + sRect.size.height - margin - rect.origin.y;
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleToParentLeft
- {
- return [self scaleToParentLeftWithMargin:0];
- }
- - (UIView *)scaleToParentLeftWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect sRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.size.width = rect.origin.x + rect.size.width - (sRect.origin.x + margin);
- rect.origin.x = sRect.origin.x + margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)scaleToParentRight
- {
- return [self scaleToParentRightWithMargin:0];
- }
- - (UIView *)scaleToParentRightWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect sRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.size.width = sRect.origin.x + sRect.size.width - margin - rect.origin.x;
- self.frame = rect;
- return self;
- }
- // 同一控件内两子之空间之间的对齐关系
- // 会影响origin的位置, 不影响大小
- - (UIView *)alignTop:(UIView *)brotherView
- {
- return [self alignTop:brotherView margin:0];
- }
- - (UIView *)alignTop:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.y = brect.origin.y - margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignBottom:(UIView *)brotherView
- {
- return [self alignBottom:brotherView margin:0];
- }
- - (UIView *)alignBottom:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.y = brect.origin.y + brect.size.height - rect.size.height - margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignLeft:(UIView *)brotherView
- {
- return [self alignLeft:brotherView margin:0];
- }
- - (UIView *)alignLeft:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- rect.origin.x = brotherView.frame.origin.x + margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignRight:(UIView *)brotherView
- {
- return [self alignRight:brotherView margin:0];
- }
- - (UIView *)alignRight:(UIView *)brotherView margin:(CGFloat)margin
- {
- IsArgInvalid(self, brotherView);
- CGRect rect = self.frame;
- CGRect brect = brotherView.frame;
- rect.origin.x = brect.origin.x + brect.size.width - rect.size.width - margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignHorizontalCenterOf:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- CGPoint bCenter = brotherView.center;
- CGPoint center = self.center;
- center.x = bCenter.x;
- self.center = center;
- return self;
- }
- - (UIView *)alignVerticalCenterOf:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- CGPoint bCenter = brotherView.center;
- CGPoint center = self.center;
- center.y = bCenter.y;
- self.center = center;
- return self;
- }
- - (UIView *)alignCenterOf:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- self.center = brotherView.center;
- return self;
- }
- - (UIView *)moveCenterTo:(CGPoint)center
- {
- self.center = center;
- return self;
- }
- - (UIView *)move:(CGPoint)vec
- {
- CGPoint c = self.center;
- c.x += vec.x;
- c.y += vec.y;
- self.center = c;
- return self;
- }
- // 与父控件对齐的关系
- // 只影响其坐标位置,不影响其大小
- - (UIView *)alignParentTop
- {
- return [self alignParentLeftWithMargin:0];
- }
- - (UIView *)alignParentBottom
- {
- return [self alignParentBottomWithMargin:0];
- }
- - (UIView *)alignParentLeft
- {
- return [self alignParentLeftWithMargin:0];
- }
- - (UIView *)alignParentRight
- {
- return [self alignParentRightWithMargin:0];
- }
- - (UIView *)alignParentCenter
- {
- return [self alignParentCenter:CGPointMake(0, 0)];
- }
- - (UIView *)alignParentCenter:(CGPoint)margin
- {
- IsSuperViewInvalid(self);
- CGRect sbounds = self.superview.bounds;
- CGPoint center = CGPointMake(CGRectGetMidX(sbounds), CGRectGetMidY(sbounds));
- center.x += margin.x;
- center.y += margin.y;
- self.center = center;
- return self;
- }
- - (UIView *)alignParentTopWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect rect = self.frame;
- rect.origin.y = self.superview.bounds.origin.y + margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignParentBottomWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect superBounds = self.superview.bounds;
- CGRect rect = self.frame;
- rect.origin.y = superBounds.origin.y + superBounds.size.height - margin - rect.size.height;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignParentLeftWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect rect = self.frame;
- rect.origin.x = self.superview.bounds.origin.x + margin;;
- self.frame = rect;
- return self;
- }
- - (UIView *)alignParentRightWithMargin:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect superBounds = self.superview.bounds;
- CGRect rect = self.frame;
- rect.origin.x = superBounds.origin.x + superBounds.size.width - margin - rect.size.width;
- self.frame = rect;
- return self;
- }
- // 与父控件的边距
- // 只影响其坐标位置,不影响其大小
- - (UIView *)marginParetnTop:(CGFloat)top bottom:(CGFloat)bottom left:(CGFloat)left rigth:(CGFloat)right
- {
- IsSuperViewInvalid(self);
- CGRect superBounds = self.superview.bounds;
- CGRect rect = CGRectZero;
- rect.origin.y = top;
- rect.size.height = superBounds.origin.y + superBounds.size.height - rect.origin.y - bottom;
- rect.origin.x = left;
- rect.size.width = superBounds.origin.x + superBounds.size.width - rect.origin.x - right;
-
- self.frame = rect;
- return self;
- }
- - (UIView *)marginParentWithEdgeInsets:(UIEdgeInsets)inset
- {
- return [self marginParetnTop:inset.top bottom:inset.bottom left:inset.left rigth:inset.right];
- }
- - (UIView *)marginParetn:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- self.frame = CGRectInset(self.superview.bounds, margin, margin);
- return self;
- }
- - (UIView *)marginParetnHorizontal:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect supRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.origin.x = supRect.origin.x + margin;
- rect.size.width = supRect.size.width - 2 * margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)marginParetnVertical:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect supRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.origin.y = supRect.origin.y + margin;
- rect.size.height = supRect.size.height - 2 * margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)marginParentTop:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect supRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.origin.y = supRect.origin.y + margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)marginParentBottom:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect supRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.size.height = supRect.origin.y + supRect.size.height - margin - rect.origin.y;
- self.frame = rect;
- return self;
- }
- - (UIView *)marginParentLeft:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect supRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.origin.x = supRect.origin.x + margin;
- self.frame = rect;
- return self;
- }
- - (UIView *)marginParentRight:(CGFloat)margin
- {
- IsSuperViewInvalid(self);
- CGRect supRect = self.superview.bounds;
- CGRect rect = self.frame;
- rect.size.width = supRect.origin.x + supRect.size.width - margin - rect.origin.x;
- self.frame = rect;
- return self;
- }
- // 控件在父控控件中的位置
- // 水平居中
- // 只影响其坐标位置,不影响其大小
- - (UIView *)layoutParentHorizontalCenter
- {
- IsSuperViewInvalid(self);
- CGPoint center = CGPointZero;
- CGRect srect = self.superview.bounds;
- center.x = CGRectGetMidX(srect);;
- center.y = self.center.y;
- self.center = center;
- return self;
- }
- // 垂直居中
- - (UIView *)layoutParentVerticalCenter
- {
- IsSuperViewInvalid(self);
- CGPoint center = CGPointZero;
- center.x = self.center.x;
- CGRect srect = self.superview.bounds;
-
- center.y = CGRectGetMidY(srect);
- self.center = center;
- return self;
- }
- // 居中
- - (UIView *)layoutParentCenter
- {
- IsSuperViewInvalid(self);
- CGRect rect = self.superview.bounds;
-
- self.center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
- return self;
- }
- // 与其他控件的大小关系
- // 影响其大小
- - (UIView *)widthEqualTo:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- CGRect brect = brotherView.frame;
- CGRect srect = self.frame;
- srect.size.width = brect.size.width;
- self.frame = srect;
- return self;
- }
- - (UIView *)heigthEqualTo:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- CGRect brect = brotherView.frame;
- CGRect srect = self.frame;
- srect.size.height = brect.size.height;
- self.frame = srect;
- return self;
- }
- - (UIView *)sizeEqualTo:(UIView *)brotherView
- {
- IsArgInvalid(self, brotherView);
- CGRect brect = brotherView.frame;
- CGRect srect = self.frame;
- srect.size = brect.size;
- self.frame = srect;
- return self;
- }
- - (UIView *)sizeWith:(CGSize)size
- {
- CGRect rect = CGRectZero;
- rect.size = size;
- self.frame = rect;
- return self;
- }
- - (UIView *)setWidth:(CGFloat)width
- {
- CGRect rect = self.frame;
- rect.size.width = width;
- self.frame = rect;
- return self;
- }
- - (UIView *)setHeight:(CGFloat)height
- {
- CGRect rect = self.frame;
- rect.size.height = height;
- self.frame = rect;
- return self;
- }
- - (UIView *)shrink:(CGSize)size
- {
- CGRect rect = self.frame;
- self.frame = CGRectInset(rect, size.width, size.height);
- return self;
- }
- - (UIView *)shrinkHorizontal:(CGFloat)margin
- {
- CGRect rect = self.frame;
- self.frame = CGRectInset(rect, margin, 0);
- return self;
- }
- - (UIView *)shrinkVertical:(CGFloat)margin
- {
- CGRect rect = self.frame;
- self.frame = CGRectInset(rect, 0, margin);
- return self;
- }
- // views里面的View都是按UI的指定顺序放好的
- - (UIView *)alignViews:(NSArray *)array isSubView:(BOOL)isSub padding:(CGFloat)padding margin:(CGFloat)margin horizontal:(BOOL)ishorizontal inRect:(CGRect)rect
- {
- BOOL isSameParent = YES;
- if (!isSub)
- {
- for (UIView *view in array)
- {
- isSameParent = isSameParent && (view.superview == self);
- if (!isSameParent)
- {
- // DebugLog(@"所排列的View的父控件不同");
- return self;
- break;
- }
- }
- }
-
- if (ishorizontal)
- {
- CGRect marginRect = CGRectInset(rect, margin, 0);
-
- NSInteger count = array.count;
-
- const CGFloat kWidth = (marginRect.size.width - (count - 1)*padding)/count;
- CGFloat startX = marginRect.origin.x;
- for (UIView *view in array)
- {
- CGRect rect = view.frame;
- rect.origin.y = marginRect.origin.y;
- rect.size.height = marginRect.size.height;
- rect.origin.x = startX;
- rect.size.width = kWidth;
- view.frame = rect;
- startX += padding + kWidth;
- }
- }
- else
- {
- CGRect marginRect = CGRectInset(rect, 0, margin);
-
- NSInteger count = array.count;
-
- const CGFloat kHeight = (marginRect.size.width - (count - 1)*padding)/count;
- CGFloat startY = marginRect.origin.y;
- for (UIView *view in array)
- {
- CGRect viewrect = view.frame;
- rect.origin.x = marginRect.origin.x;
- rect.size.width = marginRect.size.width;
- viewrect.origin.y = startY;
- viewrect.size.height = kHeight;
- view.frame = viewrect;
- startY += padding + kHeight;
- }
- }
-
-
- return self;
- }
- - (UIView *)alignSubviewsHorizontallyWithPadding:(CGFloat)padding margin:(CGFloat)margin
- {
- return [self alignViews:self.subviews isSubView:YES padding:padding margin:margin horizontal:YES inRect:self.bounds];
- }
- - (UIView *)alignSubviewsVerticallyWithPadding:(CGFloat)padding margin:(CGFloat)margin;
- {
- return[self alignViews:self.subviews isSubView:YES padding:padding margin:margin horizontal:NO inRect:self.bounds];
- }
- - (UIView *)alignSubviews:(NSArray *)views horizontallyWithPadding:(CGFloat)padding margin:(CGFloat)margin inRect:(CGRect)rect
- {
- return [self alignViews:views isSubView:NO padding:padding margin:margin horizontal:YES inRect:rect];
- }
- - (UIView *)alignSubviews:(NSArray *)views verticallyWithPadding:(CGFloat)padding margin:(CGFloat)margin inRect:(CGRect)rect
- {
- return [self alignViews:views isSubView:NO padding:padding margin:margin horizontal:NO inRect:rect];
- }
- - (UIView *)gridViews:(NSArray *)views inColumn:(NSInteger)column size:(CGSize)cellSize margin:(CGSize)margin inRect:(CGRect)rect
- {
- CGSize menuSize = cellSize;
-
- const NSInteger kColumn = column;
- const NSInteger kCount = views.count;
- const NSInteger kRow = kCount % kColumn == 0 ? kCount / kColumn : kCount / kColumn + 1;
-
- const CGSize kSpaceSize = margin;
-
- CGRect menuCellRect = rect;
- menuCellRect.size = menuSize;
-
- for (NSInteger i = 0; i < kRow; i++)
- {
- for (NSInteger j = 0; j < kColumn; j++)
- {
- NSInteger index = i * kColumn + j;
- if (index >= kCount)
- {
- return self;
- }
-
- UIView *view = [views objectAtIndex:index];
- view.frame = menuCellRect;
- menuCellRect.origin.x += menuCellRect.size.width + kSpaceSize.width;
- }
-
- menuCellRect.origin.x = rect.origin.x;
- menuCellRect.origin.y += menuCellRect.size.height + kSpaceSize.height;
- }
-
- return self;
- }
- @end
|