UIView的类别实现之设置frame

来源:互联网 发布:软件操作手册 编辑:程序博客网 时间:2024/05/16 09:53
设置frame是一件很头痛的事情,多个视图的话,需要写大量的self.frame.size.width类似的代码。我们写一个UIView的类别,来方便我们以后代码简化。#import <UIKit/UIKit.h>@interface UIView (Frame)@property (nonatomic,assign) CGFloat x;@property (nonatomic,assign) CGFloat y;@property (nonatomic,assign) CGFloat width;@property (nonatomic,assign) CGFloat height;@end#import "UIView+Frame.h"@implementation UIView (Frame)/**  设置视图x坐标 */- (void)setX:(CGFloat)x {    CGRect rect = self.frame;    rect.origin.x = x;    self.frame = rect;}/**  获取视图x坐标 */- (CGFloat)x {    return self.frame.origin.x;}/**  设置视图y坐标 */- (void)setY:(CGFloat)y {    CGRect rect = self.frame;    rect.origin.y = y;    self.frame = rect;}/**  获取视图y坐标 */- (CGFloat)y {    return self.frame.origin.y;}/**  设置视图宽度 */- (void)setWidth:(CGFloat)width {    CGRect rect = self.frame;    rect.size.width = width;    self.frame = rect;}/**  获取视图宽度 */- (CGFloat)width {    return self.frame.size.width;}/**  设置视图高度 */- (void)setHeight:(CGFloat)height {    CGRect rect = self.frame;    rect.size.height = height;    self.frame = rect;}/**  获取视图高度 */- (CGFloat)height {    return self.frame.size.height;}@end

0 0