本篇是是本人在博客園寫的第一篇博客,前幾天因為種種原因最終決定離開混了幾年的csdn。希望在博客園有個新的開始
Foundation框架裡面的frame是大家最熟悉不過的一個屬性了,但是修改起來比較麻煩,他是CGRect類型的CGRect是結構體 結構體類型裡面的某個屬性如果想要修改是不允許單個修改的,必須像下面這樣先取出,改一下再重新賦值回去,也就是大家常說的三部曲

如果結構體類型的東西也可以直接修改 那會有多爽?就像下面這樣。

其實只要自己給UIView寫個分類就好了 用這個分類來替代frame。
大概思想就是給用分類給UIView多增加幾個屬性x,y,height,width。這幾個屬性都分別實現get方法和set方法。這樣以後frame就可以離開他了
分類UIView+Frame 聲明
// // UIView+Frame.h // SXDownLoader // // Created by 董尚先 on 15/1/2. // Copyright (c) 2015年 shangxianDante. All rights reserved. // #import <UIKit/UIKit.h> @interface UIView (Frame) // 自己模仿frame寫出他的四個屬性 @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) CGFloat y; @property (nonatomic, assign) CGFloat width; @property (nonatomic, assign) CGFloat height; @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輕松的單個修改了
