gpt4 book ai didi

Swift - 如何在自定义类中设置 UIBarButtonItem 的图像

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:48 24 4
gpt4 key购买 nike

您好,我有一个 UIBarButtonImage 的自定义类:

class CheckBoxBarButton : UIBarButtonItem {

// images
let checkedImage = UIImage(named: "img_checked")! as UIImage
let unCheckedImage = UIImage(named: "img_unchecked")! as UIImage

//bool property
var isChecked: Bool = false {
didSet{
if isChecked == true {

// here I want to set the checked image for my bar button

}else{

// here I want to set the unchecked image for my bar button
}
}
}

override func awakeFromNib() {

// here I want to add a target when the button is touched.
// the actual code is returning an error because UIBarButtonImage doesnt not have addtarget
self.addTarget(self, action: #selector(CheckBoxBarButton.buttonClicked(_:)), forControlEvents: UIControlEvents.TouchUpInside)

self.isChecked = false
}
}

Any clue?

最佳答案

只需使用image 属性

var isChecked: Bool = false {
didSet{
if isChecked == true {
self.image = checkedImage
}else{
self.image = unCheckedImage
}
}
}

关于Swift - 如何在自定义类中设置 UIBarButtonItem 的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com