在这里分享一个方法
封装frame可以随时随地的修改frame的属性
前提是要新建一个pch
然后在Build Setting中搜索 prefixHeader
在后面的地址修改为pch所在的位置,pch就设置好了
然后就可以在pch中添加需要公共的类和方法了
#ifndef _1_______PrefixHeader_pch
#define _1_______PrefixHeader_pch
#import "UIView+XMGExtension.h"
#endif
UIView+XMGExtension.h的代码:
@interface UIView (XMGExtension)
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
//- (CGFloat)x;
//- (void)setX:(CGFloat)x;
/** 在分类中声明@property, 只会生成方法的声明, 不会生成方法的实现和带有_下划线的成员变量*/
@end
XMGExtension.m的代码
@implementation UIView (XMGExtension)
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
@end