原来重写也是这么简单。。。
不要怕事,要仔细的去解决。。
今天准备重写一个UIButton,试了好几次都没有成功。在技术群了请教了一下,其中就有一个大牛,帮我解决了!
想重写buttonWithType:方法,原来以为要用super = [super init]等等这样的方法。后来发现,相差很大。。
现在把我写的代码贴出来。。。与其他有同样迷惑的技术友分享一下。。
+(id)buttonWithType:(UIButtonType)type{
// [super buttonWithType:type];
// self = [super buttonWithType:type];
UIButton * btn = [super buttonWithType:type];
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
return btn;
}
其中,btn.titleEdgeInsets这样,是不可以的。。。必须得用set方法。
暂时先总结这么多。。当很多按钮有相同的特点,比如相同的背景颜色、背景图片的时候,这个就很有必要的。。。
欢迎拍砖。。