在布局UI的时候,会有大量设置UIView frame的地方,虽然系统提供了一些方法,但是还不够直接,课余之际略作总结,共享给大家,需要的直接拿走,但需要注明出处哦,不谢~~
// // UIView+Extension.h // // Created by Hunk on 15/4/13. // Copyright (c) 2015年 Hunk. All rights reserved. // #import <UIKit/UIKit.h> @interface UIView (Extension) // View's frame.origin.x @property (nonatomic, assign) CGFloat x; // View's frame.origin.y @property (nonatomic, assign) CGFloat y; // View's frame.size.width @property (nonatomic, assign) CGFloat width; // View's frame.size.height @property (nonatomic, assign) CGFloat height; // View's frame.origin @property (nonatomic, assign) CGPoint origin; // View's frame.size @property (nonatomic, assign) CGSize size; // View's center.x @property (nonatomic, assign) CGFloat centerX; // View's center.y @property (nonatomic, assign) CGFloat centerY; @end
//
// UIView+Extension.m
//
// Created by Hunk on 15/4/13.
// Copyright (c) 2015年 Hunk. All rights reserved.
//
#import "UIView+Extension.h"
@implementation UIView (Extension)
- (void)setX:(CGFloat)x
{
CGRect rect = self.frame;
rect.origin.x = x;
self.frame = rect;
}
- (CGFloat)x
{
return CGRectGetMinX(self.frame);
}
- (void)setY:(CGFloat)y
{
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
- (CGFloat)y
{
return CGRectGetMinY(self.frame);
}
- (void)setWidth:(CGFloat)width
{
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
}
- (CGFloat)width
{
return CGRectGetWidth(self.frame);
}
- (void)setHeight:(CGFloat)height
{
CGRect rect = self.frame;
rect.size.height = height;
self.frame = rect;
}
- (CGFloat)height
{
return CGRectGetHeight(self.frame);
}
- (void)setOrigin:(CGPoint)origin
{
CGRect rect = self.frame;
rect.origin = origin;
self.frame = rect;
}
- (CGPoint)origin
{
return self.frame.origin;
}
- (void)setSize:(CGSize)size
{
CGRect rect = self.frame;
rect.size = size;
self.frame = rect;
}
- (CGSize)size
{
return self.frame.size;
}
- (void)setCenterX:(CGFloat)centerX
{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
- (CGFloat)centerX
{
return self.center.x;
}
- (void)setCenterY:(CGFloat)centerY
{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
- (CGFloat)centerY
{
return self.center.y;
}
@end
原文地址:http://blog.csdn.net/lixuwen521/article/details/45023549