我试图创建一个自定义UIButton,但当我将背景设置为透明/透明时,它会用黑色填充框架的背景。我不是在IB中创建这些,我需要通过编程来完成。我缺了什么吗?
UIButton* btn = [UIButton buttonWithType: UIButtonTypeCustom];
btn.frame = rect;
[btn addTarget: self action:@selector(toggleMaster) forControlEvents: UIControlEventTouchUpInside];
btn.opaque = NO;
btn.backgroundColor = [UIColor clearColor];
[btn setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: @"%@_icon", self.currentKey]] forState: UIControlStateNormal];
[self addSubview: btn];
编辑:我意识到我应该在UIView的drawRect
方法中提到我正在做这件事。不良做法?
我认为您不需要设置backgroundColor
,默认情况下UIButtonTypeCustom
已经是透明的。你试过不用它吗?此外,图像是否透明?按钮的大小是一样的?