我有一个带有选择切换的透明图像的UIButton。在当前配置中,我使用的是系统按钮类型,因为它可以很好地颠倒按钮。然而,通过这种反转,我得到了一个带有边框的图像,并且我希望这个图像填充整个按钮。如何才能做到这一点?我想从系统中保留透明的遮罩。
下图中显示的是选定的系统状态。

发布于 2017-10-12 20:17:42
创建UIButton的子类并覆盖isSelected
例如
class CustomButton: UIButton {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
override var isSelected: Bool {
didSet {
if isSelected {
self.backgroundColor = UIColor.blue
self.tintColor = UIColor.white
} else {
self.backgroundColor = .white
self.tintColor = UIColor.red
}
}
}
}根据需要更改颜色
在视图控制器中,如下所示
class ViewController: UIViewController {
@IBOutlet weak var button: CustomButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
button.isSelected = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnTapped(_ sender: Any) {
button.isSelected = !button.isSelected
}}
还有一件事你必须将图像的渲染选项设置为模板

https://stackoverflow.com/questions/46707788
复制相似问题