// // 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