根据button的不同点击状态可以在两个背景图片之间来回切换

177 阅读1分钟

ios button 按钮的背景图片来回切换

根据button的不同点击状态可以在两个背景图片之间来回切换

let btn1 = UIButton(type: .custom)
btn1.backgroundColor = .clear
btn1.layer.cornerRadius = 5
let image1 = UIImage(named: "notSelected",in: xx, compatibleWith: nil)
let image2 = UIImage(named: "selected",in: xx, compatibleWith: nil)

// 设置不同状态下的背景图片
apaMapViewResetBtn.setBackgroundImage(image1, for: .normal)
apaMapViewResetBtn.setBackgroundImage(image2, for: .selected)

apaMapViewResetBtn.contentMode = .scaleToFill
btn1.addTarget(self, action: #selector(bnt1func1), for: .touchUpInside)

func bnt1func1(_ sender: UIButton) {
  sender.isSelected.toggle()
  //...
}