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 }}
@H_419_18@解决方法 您可以使用图像创建一个按钮,并将UIbarbuttonItem的customVIEw设置为您的按钮:Any clue?
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的图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)