很多时候我们在写控件的frame的时候都要写一长串分self.frame.size.width
,为了简化操作,我们可以抽取一个UIView分类,以后每次写farme的时候只需要写self.lhl_width
,下面是具体实现:
-
新建一个分类继承自UIView:
-
UIView+Frame.h
:此处定义属性名称的时候需要注意,为了防止和同事起重名的属性名,建议加上自己独有的前缀,以防重定义
#import <UIKit/UIKit.h>@interface UIView (Frame) @property CGFloat lhl_width; @property CGFloat lhl_height; @property CGFloat lhl_x; @property CGFloat lhl_y; @end
UIView+Frame.m
:实现UIView+Frame.h
中属性的setter和getter方法:
#import "UIView+Frame.h"
@implementation UIView (Frame)
- (void)setLhl_width:(CGFloat)lhl_width
{
CGRect rect = self.frame;
lhl_width = rect.size.width;
self.frame = rect;
}
- (CGFloat)lhl_width
{
return self.frame.size.width;
}
- (void)setLhl_height:(CGFloat)lhl_height
{
CGRect rect = self.frame;
lhl_height = rect.size.height;
self.frame = rect;
}
- (CGFloat)lhl_height
{
return self.frame.size.height;
}
- (void)setLhl_x:(CGFloat)lhl_x
{
CGRect rect = self.frame;
lhl_x = rect.origin.x;
self.frame = rect;
}
- (CGFloat)lhl_x
{
return self.frame.origin.x;
}
- (void)setLhl_y:(CGFloat)lhl_y
{
CGRect rect = self.frame;
lhl_y = rect.origin.y;
self.frame = rect;
}
- (CGFloat)lhl_y
{
return self.frame.origin.y;
}
@end