Home > database >  When I set Image inside a type of custom button (for normal), voice over mode is saying image name.
When I set Image inside a type of custom button (for normal), voice over mode is saying image name.

Time:10-22

I've created a custom button and set two images, one is for normal, and the other is for the selected mode. But the voice-over always says the normal image name text when the button is not selected. I've tried a lot but could not disable it.

When I disable the button imageView accessibility it is not working.

button.imageView?.isAccessibilityElement = false

When I disable the button accessibility, the voice-over is not working in accessibility mode.

button.isAccessibilityElement = false

If I remove the '.normal' mode image then it works, but normal mode image functionality is not considered/worked there. I'm surfing a lot. Help anyone and thanks in advance.

Code:

self.setImage(UIImage.init(named: imageName1), for: .normal)
self.setImage(UIImage.init(named: imageName1), for: .selected)

CodePudding user response:

You can do it with a simple function, this is an example...

Declare your image and your button under controller class:

 let newButton: UIButton = {
    let button = UIButton(type: .system)
    button.backgroundColor = .red
    button.tintColor = .white
    button.imageView?.contentMode = .scaleAspectFit
    button.clipsToBounds = true
    
    return button
}()

let image1 = UIImage(named: "magnifier") // image in my assets
let image2 = UIImage(named: "user") // image in my assets

in viewDidLoad addTarget to your button and call the control function, in my case:

handleCange()
newButton.addTarget(self, action: #selector(handleCange), for: .touchUpInside)

now set control variable and handleCange() func

var controlButtonState = false

@objc fileprivate func handleCange() {
    
    if controlButtonState == true {
        newButton.setImage(image1, for: .normal)
        controlButtonState = false
    } else {
        newButton.setImage(image2, for: .normal)
        controlButtonState = true
    }
}

CodePudding user response:

I found an alternative solution. I think it is a proper solution. Nonetheless, I am sharing this alternative solution. The question is open if anyone gets any proper solutions. Thanks!

import UIKit

struct RadioViewControllerConstant {
    static let dayImage = "RadioButtonDontSelect"
    static let dayImageSelected = "RadioButtonSelect"
}

class RadioViewController: UIViewController {
    
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    
    let image1 = UIImage(named: RadioViewControllerConstant.dayImageSelected)
    let image2 = UIImage(named: RadioViewControllerConstant.dayImage)
    var controlButtonState1 = false
    var controlButtonState2 = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setVO()
    }
    
    func setVO() {
        button1.accessibilityTraits = .none
        button2.accessibilityTraits = .none
        button1.isSelected = true
        button2.isSelected = true
        
        handleCange1()
        handleCange2()
        
        button1.addTarget(self, action: #selector(handleCange1), for: .touchUpInside)
        button2.addTarget(self, action: #selector(handleCange2), for: .touchUpInside)
    }
    
    @objc fileprivate func handleCange1() {
        if controlButtonState1 == true {
            button1.imageView?.accessibilityLabel = "Radio button deselected"
            button1.setImage(image2, for: .selected)
            controlButtonState1 = false
        } else {
            button1.imageView?.accessibilityLabel = "Radio button selected"
            button1.setImage(image1, for: .selected)
            controlButtonState1 = true
        }
    }
    
    @objc fileprivate func handleCange2() {
        if controlButtonState2 == true {
            button2.imageView?.accessibilityLabel = "Radio button deselected"
            button2.setImage(image2, for: .selected)
            controlButtonState2 = false
        } else {
            button2.imageView?.accessibilityLabel = "Radio button selected"
            button2.setImage(image1, for: .selected)
            controlButtonState2 = true
        }
    }
}
  • Related