iOS 直接改变控件x.y.width.height

// 1.先创建UIView的category 2.代码拷贝进去 3.引用

#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;

@property (nonatomic,assign)CGPoint origin;

@property (nonatomic,assign)CGSize size;

@end



@implementation UIView (Frame)


- (void)setX:(CGFloat)x

{

    CGRect frame = self.frame;

    frame.origin.x = x;

    self.frame = frame;

}


- (CGFloat)x

{

    returnself.frame.origin.x;

}


- (void)setY:(CGFloat)y

{

    CGRect frame = self.frame;

    frame.origin.y = y;

    self.frame = frame;

}


- (CGFloat)y

{

    returnself.frame.origin.y;

}


- (void)setOrigin:(CGPoint)origin

{

    CGRect frame = self.frame;

    frame.origin = origin;

    self.frame = frame;

}


- (CGPoint)origin

{

    returnself.frame.origin;

}


- (void)setWidth:(CGFloat)width

{

    CGRect frame = self.frame;

    frame.size.width = width;

    self.frame = frame;

}


- (CGFloat)width

{

    returnself.frame.size.width;

}


- (void)setHeight:(CGFloat)height

{

    CGRect frame = self.frame;

    frame.size.height = height;

    self.frame = frame;

}


- (CGFloat)height

{

    returnself.frame.size.height;

}


- (void)setSize:(CGSize)size

{

    CGRect frame = self.frame;

    frame.size = size;

    self.frame = frame;

}


- (CGSize)size

{

    return self.frame.size;

}



@end



    // 使用方法

    UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(0,80,50, 10)];

    label.text =@"1234564784684864";

    label.height = 20;

    [self.view addSubview:label];






猜你喜欢

转载自blog.csdn.net/saw471/article/details/77944067
今日推荐