标签:
很多时候,如果我们想给我们的控件赋值,例如给控件的长度、宽度等赋值,很麻烦
需要先获取到当前frame,再整个临时frame来保存,修改赋值后再还给当前的frame,这都是重复性高的苦力活,解决方法就是写一个类别,这些步骤都挂起来,大家一起用。
话不多说看类别:
头文件:
1 // 2 // UIView+Extension.h 3 // XibaTest 4 // 5 // Created by bos on 15-6-11. 6 // Copyright (c) 2015年 axiba. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @interface UIView (Extension) 12 13 @property (nonatomic,assign) CGFloat x; 14 @property (nonatomic,assign) CGFloat y; 15 @property (nonatomic,assign) CGFloat centerX; 16 @property (nonatomic,assign) CGFloat centerY; 17 @property (nonatomic,assign) CGFloat width; 18 @property (nonatomic ,assign) CGFloat height; 19 @property (nonatomic, assign) CGSize size; 20 @property (nonatomic,assign) CGPoint orign; 21 22 @end
代码文件:
1 // 2 // UIView+Extension.m 3 // XibaTest 4 // 5 // Created by bos on 15-6-11. 6 // Copyright (c) 2015年 axiba. All rights reserved. 7 // 8 9 #import "UIView+Extension.h" 10 11 @implementation UIView (Extension) 12 13 -(void)setX:(CGFloat)x 14 { 15 CGRect frame = self.frame; 16 frame.origin.x = x; 17 self.frame = frame; 18 } 19 -(CGFloat)x 20 { 21 return self.frame.origin.x; 22 } 23 24 -(void)setY:(CGFloat)y 25 { 26 CGRect frame = self.frame; 27 frame.origin.y = y; 28 29 self.frame = frame; 30 } 31 -(CGFloat)y 32 { 33 return self.frame.origin.y; 34 } 35 36 -(void)setCenterX:(CGFloat)centerX 37 { 38 CGPoint center = self.center; 39 center.x = centerX; 40 self.center = center; 41 } 42 -(CGFloat)centerX 43 { 44 return self.center.x; 45 } 46 47 -(void)setCenterY:(CGFloat)centerY 48 { 49 CGPoint center = self.center; 50 center.y = centerY; 51 self.center = center; 52 } 53 -(CGFloat)centerY 54 { 55 return self.center.y; 56 } 57 58 -(void)setWidth:(CGFloat)width 59 { 60 CGRect frame = self.frame; 61 frame.size.width = width; 62 63 self.frame = frame; 64 } 65 -(CGFloat)width 66 { 67 return self.frame.size.width; 68 } 69 70 -(void)setHeight:(CGFloat)height 71 { 72 CGRect frame = self.frame; 73 frame.size.height = height; 74 75 self.frame = frame; 76 } 77 -(CGFloat)height 78 { 79 return self.frame.size.height; 80 } 81 82 -(void)setSize:(CGSize)size 83 { 84 CGRect frame = self.frame; 85 frame.size =size; 86 self.frame = frame; 87 } 88 -(CGSize)size 89 { 90 return self.frame.size; 91 } 92 93 -(void)setOrign:(CGPoint)orign 94 { 95 CGRect frame = self.frame; 96 frame.origin = orign; 97 98 self.frame = frame; 99 } 100 -(CGPoint)orign 101 { 102 return self.frame.origin; 103 } 104 105 @end
写完类别,记得引入,可以在个别控制器中单独引入,也可以在pch文件中 #import "UIView+Extension.h" ,大家一起用。
标签:
原文地址:http://www.cnblogs.com/daomul/p/4662402.html