
本篇是是本人在博客园写的第一篇博客,前几天因为种种原因最终决定离开混了几年的csdn。希望在博客园有个新的开始
Foundation框架里面的frame是大家最熟悉不过的一个属性了,但是修改起来比较麻烦,他是CGRect类型的CGRect是结构体 结构体类型里面的某个属性如果想要修改是不允许单个修改的,必须像下面这样先取出,改一下再重新赋值回去,也就是大家常说的三部曲

如果结构体类型的东西也可以直接修改 那会有多爽?就像下面这样。

其实只要自己给UIView写个分类就好了 用这个分类来替代frame。
大概思想就是给用分类给UIView多增加几个属性x,y,height,width。这几个属性都分别实现get方法和set方法。这样以后frame就可以离开他了
分类UIView+Frame 声明
1 // 2 // UIView+Frame.h 3 // SXDownLoader 4 // 5 // Created by 董尚先 on 15/1/2. 6 // Copyright (c) 2015年 shangxianDante. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface UIView (Frame) 12 13 // 自己模仿frame写出他的四个属性 14 @PRoperty (nonatomic, assign) CGFloat x; 15 @property (nonatomic, assign) CGFloat y; 16 @property (nonatomic, assign) CGFloat width; 17 @property (nonatomic, assign) CGFloat height; 18 19 20 @end
分类UIView+Frame 实现
#import "UIView+Frame.h"
@implementation UIView (Frame)
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
@end
之后在需要的地方Import一下
就可以把那些UI控件什么的frame轻松的单个修改了
