123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- //
- // UIView+LayoutMethods.m
- // TmallClient4iOS-Prime
- //
- // Created by casa on 14/12/8.
- // Copyright (c) 2014年 casa. All rights reserved.
- //
- #import "UIView+LayoutMethods.h"
- @implementation UIView (LayoutMethods)
- // coordinator getters
- - (CGFloat)height
- {
- return self.frame.size.height;
- }
- - (CGFloat)width
- {
- return self.frame.size.width;
- }
- - (CGFloat)x
- {
- return self.frame.origin.x;
- }
- - (void)setX:(CGFloat)x
- {
- self.frame = CGRectMake(x, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
- }
- - (void)setY:(CGFloat)y
- {
- self.frame = CGRectMake(self.frame.origin.x, y, self.frame.size.width, self.frame.size.height);
- }
- - (CGFloat)y
- {
- return self.frame.origin.y;
- }
- - (CGSize)size
- {
- return self.frame.size;
- }
- - (CGPoint)origin
- {
- return self.frame.origin;
- }
- - (CGFloat)centerX
- {
- return self.center.x;
- }
- - (CGFloat)centerY
- {
- return self.center.y;
- }
- - (CGFloat)bottom
- {
- return self.frame.size.height + self.frame.origin.y;
- }
- - (CGFloat)right
- {
- return self.frame.size.width + self.frame.origin.x;
- }
- - (void)setCornerRadius:(CGFloat)cornerRadius
- {
- UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
-
- CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
- //设置大小
- maskLayer.frame = self.bounds;
- //设置图形样子
- maskLayer.path = maskPath.CGPath;
- self.layer.mask = maskLayer;
- }
- // height
- - (void)setHeight:(CGFloat)height
- {
- CGRect newFrame = CGRectMake(self.x, self.y, self.width, height);
- self.frame = newFrame;
- }
- - (void)heightEqualToView:(UIView *)view
- {
- self.height = view.height;
- }
- // width
- - (void)setWidth:(CGFloat)width
- {
- CGRect newFrame = CGRectMake(self.x, self.y, width, self.height);
- self.frame = newFrame;
- }
- - (void)widthEqualToView:(UIView *)view
- {
- self.width = view.width;
- }
- // center
- - (void)setCenterX:(CGFloat)centerX
- {
- CGPoint center = CGPointMake(self.centerX, self.centerY);
- center.x = centerX;
- self.center = center;
- }
- - (void)setCenterY:(CGFloat)centerY
- {
- CGPoint center = CGPointMake(self.centerX, self.centerY);
- center.y = centerY;
- self.center = center;
- }
- - (void)centerXEqualToView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewCenterPoint = [superView convertPoint:view.center toView:self.topSuperView];
- CGPoint centerPoint = [self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
- self.centerX = centerPoint.x;
- }
- - (void)centerYEqualToView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewCenterPoint = [superView convertPoint:view.center toView:self.topSuperView];
- CGPoint centerPoint = [self.topSuperView convertPoint:viewCenterPoint toView:self.superview];
- self.centerY = centerPoint.y;
- }
- // top, bottom, left, right
- - (void)top:(CGFloat)top FromView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.y = newOrigin.y + top + view.height;
- }
- - (void)bottom:(CGFloat)bottom FromView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.y = newOrigin.y - bottom - self.height;
- }
- - (void)left:(CGFloat)left FromView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.x = newOrigin.x - left - self.width;
- }
- - (void)right:(CGFloat)right FromView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.x = newOrigin.x + right + view.width;
- }
- - (void)topInContainer:(CGFloat)top shouldResize:(BOOL)shouldResize
- {
- if (shouldResize) {
- self.height = self.y - top + self.height;
- }
- self.y = top;
- }
- - (void)bottomInContainer:(CGFloat)bottom shouldResize:(BOOL)shouldResize
- {
- if (shouldResize) {
- self.height = self.superview.height - bottom - self.y;
- } else {
- self.y = self.superview.height - self.height - bottom;
- }
- }
- - (void)leftInContainer:(CGFloat)left shouldResize:(BOOL)shouldResize
- {
- if (shouldResize) {
- self.width = self.x - left + self.superview.width;
- }
- self.x = left;
- }
- - (void)rightInContainer:(CGFloat)right shouldResize:(BOOL)shouldResize
- {
- if (shouldResize) {
- self.width = self.superview.width - right - self.x;
- } else {
- self.x = self.superview.width - self.width - right;
- }
- }
- - (void)topEqualToView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.y = newOrigin.y;
- }
- - (void)bottomEqualToView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.y = newOrigin.y + view.height - self.height;
- }
- - (void)leftEqualToView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.x = newOrigin.x;
- }
- - (void)rightEqualToView:(UIView *)view
- {
- UIView *superView = view.superview ? view.superview : view;
- CGPoint viewOrigin = [superView convertPoint:view.origin toView:self.topSuperView];
- CGPoint newOrigin = [self.topSuperView convertPoint:viewOrigin toView:self.superview];
-
- self.x = newOrigin.x + view.width - self.width;
- }
- // size
- - (void)setSize:(CGSize)size
- {
- self.frame = CGRectMake(self.x, self.y, size.width, size.height);
- }
- - (void)sizeEqualToView:(UIView *)view
- {
- self.frame = CGRectMake(self.x, self.y, view.width, view.height);
- }
- // imbueset
- - (void)fillWidth
- {
- self.width = self.superview.width;
- }
- - (void)fillHeight
- {
- self.height = self.superview.height;
- }
- - (void)fill
- {
- self.frame = CGRectMake(0, 0, self.superview.width, self.superview.height);
- }
- - (UIView *)topSuperView
- {
- UIView *topSuperView = self.superview;
-
- if (topSuperView == nil) {
- topSuperView = self;
- } else {
- while (topSuperView.superview) {
- topSuperView = topSuperView.superview;
- }
- }
-
- return topSuperView;
- }
- - (void)removeAllSubviews {
- while (self.subviews.count) {
- UIView* child = self.subviews.lastObject;
- [child removeFromSuperview];
- }
- }
- @end
|