构建一个基于UIView的类别

  很多时候,如果我们想给我们的控件赋值,例如给控件的长度、宽度等赋值,很麻烦

  需要先获取到当前frame,再整个临时frame来保存,修改赋值后再还给当前的frame,这都是重复性高的苦力活,解决方法就是写一个类别,这些步骤都挂起来,大家一起用。

  话不多说看类别:

头文件:

  

 //
// UIView+Extension.h
// XibaTest
//
// Created by bos on 15-6-11.
// Copyright (c) 2015年 axiba. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (Extension) @property (nonatomic,assign) CGFloat x;
@property (nonatomic,assign) CGFloat y;
@property (nonatomic,assign) CGFloat centerX;
@property (nonatomic,assign) CGFloat centerY;
@property (nonatomic,assign) CGFloat width;
@property (nonatomic ,assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@property (nonatomic,assign) CGPoint orign; @end

代码文件:

 //
// UIView+Extension.m
// XibaTest
//
// Created by bos on 15-6-11.
// Copyright (c) 2015年 axiba. All rights reserved.
// #import "UIView+Extension.h" @implementation UIView (Extension) -(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)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;
} -(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;
} -(void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size =size;
self.frame = frame;
}
-(CGSize)size
{
return self.frame.size;
} -(void)setOrign:(CGPoint)orign
{
CGRect frame = self.frame;
frame.origin = orign; self.frame = frame;
}
-(CGPoint)orign
{
return self.frame.origin;
} @end

  写完类别,记得引入,可以在个别控制器中单独引入,也可以在pch文件中  #import "UIView+Extension.h" ,大家一起用。

上一篇:Wireshark for Ethical Hackers - 2


下一篇:请做一个Filter过滤器的hello world最简单的一个例子