Setting UIButton Image With Ternary Operator - ios

I have a password UICollectionViewCell with a UITextField to enter the password. I want to have a button where the user can change the isSecureTextEntry property.
My Code
private lazy var button: UIButton = {
let view = UIButton()
view.translatesAutoresizingMaskIntoConstraints = false
view.addTarget(self, action: #selector(toggle), for: .touchUpInside)
return view
}()
#objc func toggle(_ sender: UIButton) {
sender.isSelected == false ? button.setImage(secureImage, for: .normal) : button.setImage(unSecureImage, for: .selected)
}
Set Up Cell
self.toggle(button)
However, my button image is not changing.

sender.isSelected == false ? button.setImage(secureImage, for: .normal) : button.setImage(unSecureImage, for: .selected)
You're setting for normal state on one side and for selected state on the other.
Try this:
button.setImage((sender.isSelected ? unSecureImage : secureImage), for: .normal)

Can you try like this,
Setting the image for normal and selected state
On tapping the button changing the isSelected state of the button
private lazy var button: UIButton = {
let view = UIButton()
view.translatesAutoresizingMaskIntoConstraints = false
view.setImage(secureImage, for: .normal)
view.setImage(unSecureImage, for: .selected)
view.addTarget(self, action: #selector(toggleAction), for: .touchUpInside)
return view
}()
#objc func toggleAction(_ sender: UIButton) {
sender.isSelected.toggle()
}

Related

When I set Image inside a type of custom button (for normal), voice over mode is saying image name. How can I disable it for the button imageview?

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)
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
}
}
Basically, it is not possible indirect way. On the other hand we can use accessibilityLabel
I found an alternative solution. I think it is not 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
}
}
}

Single selection radioButton

So I have 3 buttons
override func viewDidLoad() {
super.viewDidLoad()
weeklyRadioButton.setImage(UIImage(named: "a"), for: .normal)
weeklyRadioButton.setImage(UIImage(named: "b"), for: .selected)
biWeeklyRadioButton.setImage(UIImage(named: "a"), for: .normal)
biWeeklyRadioButton.setImage(UIImage(named: "b"), for: .selected)
noThanksRadioButton.setImage(UIImage(named: "a"), for: .normal)
noThanksRadioButton.setImage(UIImage(named: "b"), for: .selected)
}
#IBAction func biWeeklyRadioButtonTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
weeklyRadioButton.setImage(UIImage(named: "a"), for: .normal) .
// its not changing then image of the button.
noThanksRadioButton.setImage(UIImage(named: "a"), for: .normal)
}
and similar two Actions for other two buttons.
Current code allows multiple selection of button.
I want to know the way to implement to single selection in buttons.
so lets say i selected button 1
on selecting button 2, button 1 gets deselected
Try the below code. Please also make a note that all of the button outlets should be connected to the below action method.
#IBAction func anyRadioButtonTapped(_ sender: UIButton) {
//Reset all of them first
weeklyRadioButton.isSelected = false
biWeeklyRadioButton.isSelected = false
noThanksRadioButton.isSelected = false
//Highlight associate button
sender.isSelected = !sender.isSelected
}

How to prevent selected UIButton from changing to normal image when touching down?

I have two images for a UIButton. In its notmal state, it's a pencil. When I select it, it's a paper. But right when I touch down it shows the pencil even though I haven't selected it yet. How can I prevent this?
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
btn.setImage(#imageLiteral(resourceName: "Pencil").withRenderingMode(.alwaysTemplate), for: .normal)
btn.setImage(#imageLiteral(resourceName: "Paper").withRenderingMode(.alwaysTemplate), for: .selected)
btn.tintColor = .cyan
btn.addTarget(self, action: #selector(touchedButton), for: .touchUpInside)
}
func touchedButton() {
if !btn.isSelected {
btn.isSelected = true
} else {
btn.isSelected = false
}
}
u can use highlighted sate of UIButton for example, set image for highlighted state also,
btn.setImage(#imageLiteral(resourceName:
"Paper").withRenderingMode(.alwaysTemplate), for: .highlighted)

I want to change UIButton Image when tapped on it

I have 2 buttons in header on CollectionViewController. When i tap on one of them i'm changing an image of this buttons using UIControlState -> .normal .selected.
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
engSwitchButton.setImage(#imageLiteral(resourceName: "abc"), for: .normal)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg"), for: .normal)
engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.selected)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.selected)
engSwitchButton.tag = Language.english.rawInt
geoSwitchButton.tag = Language.georgian.rawInt
}
#IBAction func languageSwitchTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
selectedLanguage = Language(rawInt: sender.tag)!
collectionView.reloadData()
}
I want the button which i tapped first, get back to .normal state when i'm changing state of 2d button with tapping on it.
To change a button image on tap you should use the UIControlState.highlighted.
So change:
engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.selected)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.selected)
To this:
engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.highlighted)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.highlighted)
#IBAction func yourFirstButton(_ sender: Any) {
firstButton.setImage(UIImage(named: "yourButtonPressedImage")!, for: .normal)
secondbutton.setImage(UIImage(named: "yourNormalImage")!, for: .normal)
}
and in your second buttons #IBAction method just switch the images for the buttons
#IBAction func yourSecondButton(_ sender: Any) {
secondButton.setImage(UIImage(named: "yourButtonPressedImage")!, for: .normal)
firsBbutton.setImage(UIImage(named: "yourNormalImage")!, for: .normal)
}

Select/deselect buttons swift xcode 7

Part way done with learning swift but I hit a small wall and yet again, I'm sure I'm just a bit new at this and an easy solution is there but I'm having trouble figuring out how to select/deselect buttons below is what I have so far and it is a button turns into a checkmark when clicked on... I've gotten that far but I need that button to deselect when clicked on again and then obviously be able to be clicked again if need be.
#IBAction func buttonPressed(sender: AnyObject) {
sender.setImage(UIImage(named: "Checkmark.png"), forState: .Normal)
}
Swift 3 note: .selected and .checked are now lower case UIControlState values in the SDK, and some of the methods have been renamed:
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
You can also now use image literals with Xcode 8 instead of UIImage(named:):
#imageLiteral(resourceName: "Unchecked")
Swift 2:
Why not use the .Selected state of the button as the "checked" state, and the .Normal state as the "unchecked" state.
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
// ...
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender as? UIButton {
if button.selected {
// set deselected
button.selected = false
} else {
// set selected
button.selected = true
}
}
}
You dont need to set selected in condition. I just doing with following method in swift:
func selectDeselect(sender: UIButton){
sender.selected = !sender.selected
if(sender.selected == true)
{
sender.setImage(UIImage(named:"select_heart"), forState: UIControlState.Normal)
}
else
{
sender.setImage(UIImage(named:"heart"), forState: UIControlState.Normal)
}
}
Here is Working code for swift 4.
Make Sure you need to connect Button IBAction Outlet as UIButton and set default button image from storyboard whatever you want.
#IBAction func btnTapped(_ sender: UIButton) {
if sender.currentImage == UIImage(named: "radio_unchecked"){
sender.setImage(UIImage(named: "radio_checked"), for: .normal)
}else{
sender.setImage(UIImage(named: "radio_unchecked"), for: .normal)
}
}
update for Swift 4+ :
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender {
if button.isSelected {
// set deselected
button.isSelected = false
} else {
// set selected
button.isSelected = true
}
}
}
Set uncheck image on default state from storyboard and check image on selected state from storyboard.
#IBAction func buttonPressed(sender: AnyObject) {
buttonOutlet.isSelected = !buttonOutlet.isSelected
}
private(set) lazy var myButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc
func buttonTapped(sender: AnyObject) {
sender.isSelected.toggle()
}
To select only the pressed button out of three buttons and deselect the others when designated button is pressed.
#IBAction func buttonPressed(_ sender: UIButton) {
// Deselect all buttons
button1.isSelected = false
button2.isSelected = false
button3.isSelected = false
// Select the pressed button
sender.isSelected = true
}

Resources