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

Swift – 如何在自定义类中设置UIBarButtonItem的图像,第1张

概述嗨,我有一个UIBarButton Image的自定义类: class CheckBoxBarButton : UIBarButtonItem { // images let checkedImage = UIImage(named: "img_checked")! as UIImage let unCheckedImage = UIImage(named: "img_u 嗨,我有一个UIbarbutton Image的自定义类:

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?

@H_419_18@解决方法 您可以使用图像创建一个按钮,并将UIbarbuttonItem的customVIEw设置为您的按钮:

let button = UIbutton()button.setimage(checkedImage )// or button.setimage(unCheckedImage) based on the conditionsbutton.frame = CGRectMake(0,30,30)self.customVIEw = button

或者,如果已经定义了按钮,则设置customVIEw图像:

self.customVIEw.setimage(checkedImage )
总结

以上是内存溢出为你收集整理的Swift – 如何在自定义类中设置UIBarButtonItem的图像全部内容,希望文章能够帮你解决Swift – 如何在自定义类中设置UIBarButtonItem的图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001619.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存