重写 button 的创建方法

重写 button 的创建方法

//sxc时时改变
// self.videoM.progress = progress;
// if ([self.videoM.downloadStr isEqualToString:@"100%"]) {
// self.downloadBtn.titleLabel. text = @"已缓存";

  • //  }else{

  • //  self.downloadBtn.titleLabel.text = self.videoM.downloadStr;

// NSLog(@"%@=---------",self.v ideoM.downloadStr);
// }//有两种重写方法,主要看你想怎么去创建

这个button -(void)setFrame:(CGRect)frame {

[super setFrame:frame];
[self.icon removeFromSuperview]; [self.textLabel removeFromSuperview]; [self.iconSelected

removeFromSuperview];

//创建一个正方形 的imageView (只读的不能 self. set方法不可以)

_icon= [[UIImageViewalloc] initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.height)];

[self addSubview:self.icon];

_iconSelected= [[UIImageViewalloc] initWithFrame:self.icon.bounds];

self.iconSelected.hidden = YES; [self addSubview:self.iconSelected];

_textLabel= [[UILabelalloc] initWithFrame:CGRectMake(frame.size.height, 0, frame.size.width- frame.size.height, frame.size.height)];

_textLabel.textColor= [UIColor whiteColor];

_textLabel.font= [UIFont systemFontOfSize:14];

[self addSubview:self.textLabel]; //第二种

//装逼的创建方法 只执行一次,单例方法也可用

//
//
//
initWithFrame:CGRectMake(0, 0, frame.size.height, frame.size.height)];

  • //  [self addSubview:self.icon]; //

    //

  • //  _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.height, 0, frame.size.width - frame.size.height, frame.size.height)];

// [self addSubview:self.textLabel]; //

static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{

_icon = [[UIImageView alloc]

// });

}

#pragma mark 重写selected方法,做图片更换 - (void)setSelected:(BOOL)selected
{

//父类怎么做 还怎么做我们只是在它原有的基础 上添加自己的方法

[super setSelected:selected]; //根据selected属性对图片做修改 if(selected) {

self.iconSelected.hidden = NO;

self.icon.hidden = YES; }else

{
self.iconSelected.hidden = YES; self.icon.hidden = NO;

} }

//使用alloc创建
- (instancetype)initWithFrame:(CGRect)frame {

self= [superinitWithFrame:frame]; if(self) {

}
return self;

原文地址:https://www.cnblogs.com/lidongxiao/p/4950001.html