给UIView新建一个分类,方便修改frame。代码如下:
```
// UIView+Frame.h
#import <UIKit/UIKit.h>
@interface UIView (Frame)
@property (nonatomic, assign) CGSize size;
@property(nonatomic,assign)CGFloat width;
@property(nonatomic,assign)CGFloat height;
@property(nonatomic,assign)CGFloat x;
@property(nonatomic,assign)CGFloat y;
@end
```
```
// UIView+Frame.m
#import "UIView+Frame.h"
@implementation UIView (Frame)
- (void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
- (CGSize)size
{
return self.frame.size;
}
- (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)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;
}
@end
```
然后使用的时候:
本文介绍了一个为 UIView 新增的分类,通过该分类可以更便捷地调整视图的大小和位置属性,例如宽度、高度及坐标等。

被折叠的 条评论
为什么被折叠?



