iOS之重写frame和bounds的实用价值

来源:互联网 发布:大学生兼职数据调查 编辑:程序博客网 时间:2024/06/05 01:21

介绍一下自定义控件时重写frame和bounds的实用性

  • 自己可以自由修改控件的大小
  • 防止别人在使用你的自定义控件时任意修改你的自定义控件的大小

比如我新创建一个UIView类的控件,在 .m文件里重写frame和bounds

.h文件

#import <UIKit/UIKit.h>@interface GDGTestView : UIView+ (instancetype)testView;@end

.m文件

#import "GDGTestView.h"@implementation GDGTestView+ (instancetype)testView{    return [[self alloc] init];}- (void)setFrame:(CGRect)frame{    frame.size = CGSizeMake(100, 100);    [super setFrame:frame];}- (void)setBounds:(CGRect)bounds{    bounds.size = CGSizeMake(100, 100);    [super setBounds:bounds];}

写上setFrame和setBounds方法后testView的大小永远是100x100,别人再写以下代码时不能改变你控件的大小

    testView.frame = CGRectMake(0, 0, 10, 10);    testView.bounds = CGRectMake(0, 0, 10, 10);
1 0
原创粉丝点击