Button with Image, Image Disappear when clicking the button iOS 10 - ios

#IBAction func btnPrimaryAction(_ sender: Any) {
btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.normal)
btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal)
btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
payType = 1
}
I'm using this code to change the button image when clicking the button. But button image disappear when clicking the button in iOS 10.
Please Help me with this
Thanks is advance

I think this is efficient way to use UIButton State. UIButton have multiple state like UIControlState.normal,UIControlState.selected,UIControlState.highlighted etc..
You can solve your problem easily by using UIButton's UIControlState.
First you need to set Image to your button like this way.
In viewDidLoad method.
// if you have add the "checkbox_off.png and checkbox_on.png" in Images.xcassets then get the image you need to write as below
//UIImage(named : "checkbox_off") no need to add extention like ".png"
btnPrimary.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal)
btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
// Selected
btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected)
btnSecondary.setImage(UIImage(named : "checkbox_on.png"), for: UIControlState.selected)
btnNewAdd.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected)
Now in your function just change the button state
#IBAction func btnPrimaryAction(_ sender: Any) {
btnPrimary.selected = true
btnSecondary.selected = false
btnNewAdd.selected = false
payType = 1
}

Related

Remove button when click itselt in swift 3

Hello I have a custom UIButton has added. And I want to remove this button when click on it self. I have done like this.
btnDelete.addTarget(self, action: #selector(deleteCoveringPerson(sender:)), for: .touchUpInside)
btnDelete.setImage(UIImage.init(named: "close-dark"), for: .normal)
btnCoveringPerson.addSubview(btnDelete)
And this is my delete button selector
func deleteCoveringPerson(sender:UIButton)
{
dm.strCoveringPersonNAme=""
dm.strcoveringPersonCode="0"
btnCoveringPerson.setTitle(lan.getConvertedLanguageString(word: "COVERINGPERSON"), for: .normal)
btnDelete.removeFromSuperview()
}
How can I do this?
For me this work just fine
func deleteCoveringPerson(sender:UIButton)
{
dm.strCoveringPersonNAme=""
dm.strcoveringPersonCode="0"
btnCoveringPerson.setTitle(lan.getConvertedLanguageString(word: "COVERINGPERSON"), for: .normal)
sender.removeFromSuperview()
}
Hope this helps

Can't change UIbutton image for different state

I want to change the image of a UIButton for different states. To achieve this, I'm using:
btn.setImage(UIImage(named: "blabla"), for .normal)
and
btn.setImage(UIImage(named: blabla2), for .disabled)
This only makes some appear dimmed.
What did I do wrong? I just want to make my button appearance the same for different states, how?
(my button type - .system).
This helped me (swift 3.0)
btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .normal)
btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .disabled)
You just need to set one for the state. And if you don't set another image for different state. It would look the same in all state.
button.setImage(image, forState: .Normal)
How to change UIButton image in Swift
For display disabled button set image
let btn = UIButton(type: .Custom)
btn.setImage(UIImage(named: blabla2), for .disabled)
Then
btn.enabled = false // to display Disable image
btn.enabled = true // to display Normal image
private let button1: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named:"firstButtonNormalStateImage"), for: .normal)
button.setImagesetImage(UIImage(named:"firstButtonSelectedStateImage"), for: .selected)
return button
}()
private let button2: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named:"secondButtonNormalStateImage"), for: .normal)
button.setImage(UIImage(named:"secondButtonSelectedStateImage"), for: .selected)
return button
}()
// implement for example in viewDidLoad()
button1.addTarget(self, action: #selector(firstButtonDidTap), for: .touchUpInside)
button2.addTarget(self, action: #selector(secondButtonDidTap), for: .touchUpInside)
// trigger actions
#objc func firstButtonDidTap() {
button1.isSelected = true
button2.isSelected = false
}
#objc func secondButtonDidTap() {
button2.isSelected = true
button1.isSelected = false
}
For whoever is still having this issue (currently Xcode 10.0) with a Custom button, I found I was able to change the text and/or image if instead of:
myButton.setTitle("Hi", for: [.normal])
I used this:
myButton.setTitle("Hi", for: []) //remove specific states
I don't know why .normal was not working for me, even though the button was definitely enabled. But maybe this will save someone else a headache!
You can simply do this by StoryBoard as well.
Select the button, got to identity inspector and do the following:-
Firstly set the buttonType to custom instead of system.
Secondly choose state Config to lets say default and give the imageName in "image" attribute, similarly choose other state configs (Highlighted, disabled, selected etc.) and set images as required by you.
Then later in the code you just have to control and set the state of the button, and respective image will be shown to you.

Button Won’t Change Image On-Click After User Goes to Home Screen

I have a 5 different buttons where I want the user to be able to select one if it’s not selected, and de-select it if it already is selected, while at the same time de-selecting another button if it is selected.
normal = button’s image when not selected filled = button’s image when selected
To do this I have created an if/else statement:
#IBAction func option1ButtonAction(sender: AnyObject) {
if (option1Button.currentImage == UIImage(named: "normal")) {
if option2Button.currentImage == UIImage(named: "filled") || option3Button.currentImage == UIImage(named: "filled") || option4Button.currentImage == UIImage(named: "filled") || option5Button.currentImage == UIImage(named: "filled") {
option1Button.setImage(filled, forState: .Normal)
option2Button.setImage(normal, forState: .Normal)
option3Button.setImage(normal, forState: .Normal)
option4Button.setImage(normal, forState: .Normal)
option5Button.setImage(normal, forState: .Normal)
}
else {
option1Button.setImage(filled, forState: .Normal)
}
}
else {
option1Button.setImage(normal, forState: .Normal)
}
}
Afterwards I made an if option1Button.currentImage == UIImage(named: "filled”) statement. Everything works exactly how I want it to, however I have one problem. Whenever the user presses the home button and then goes right back into the app, the buttons still have the “normal” image, even when clicked.
Inside the viewDidDisappear function I put the following code in hopes to fix this issue:
option1Button.setImage(normal, forState: .Normal)
option2Button.setImage(normal, forState: .Normal)
option3Button.setImage(normal, forState: .Normal)
option4Button.setImage(normal, forState: .Normal)
option5Button.setImage(normal, forState: .Normal)
But I still have the problem occurring. I would be grateful for any help provided.
You need change image from your button when clicked! I understand right?
For your issue I believe you can do different. I make a code for example.
First set image for state in button.
option1Button.setImage(normal, forState: UIControlState.Normal);
option1Button.setImage(filled, forState: UIControlState.Disabled);
option2Button.setImage(normal, forState: UIControlState.Normal);
option2Button.setImage(filled, forState: UIControlState.Disabled);
Second I made a function, this function change state of your button.
selectButton(button:UIButton)
{
option1Button.enabled = button != option1Button;
option2Button.enabled = button != option2Button;
}
Now you just do implement this way you action, and other detail is you don't need more create a function for every button ex. #IBAction func option1ButtonAction(sender: AnyObject) only create one action #IBAction func onButtonClicked(sender: AnyObject) and like all buttons here... remember only increment your buttons in your selectButton(button:UIButton) function, or if preferrer you can create a collection and push your button there and do a while in selectButton
#IBAction func onButtonClicked(sender: AnyObject) {
if let clickedButton = sender as? UIButton{
selectButton(clickedButton);
}
}
If I understood correctly your issue, I hope helped you!

How to change button text in Swift Xcode 6?

Here's what I'm trying to do. If you've ever played Halo or CoD, you'd know that you could change the name of a weapon load-out.
What I'm doing is making it so you can change your load-out name using a text field. Here's the problem, the load-out name in the load-out menu is a button (to select and view info about that load-out) and I could just write this:
#IBAction func renameClassButton(sender: AnyObject) {
classTopButton.text = "\(classTopTextField)"
}
Except it [classTopButton] is a button which doesn't allow the '.text' suffix
You can do:
button.setTitle("my text here", forState: .normal)
Swift 3, 4, and 5:
button.setTitle("my text here", for: .normal)
In Xcode 8 - Swift 3:
button.setTitle( "entertext" , for: .normal )
It is now this For swift 3,
let button = (sender as AnyObject)
button.setTitle("Your text", for: .normal)
(The constant declaration of the variable is not necessary just make sure you use the sender from the button like this) :
(sender as AnyObject).setTitle("Your text", for: .normal)
Remember this is used inside the IBAction of your button.
NOTE:
line
someButton.setTitle("New Title", forState: .normal)
works only when Title type is Plain.
swift 4 work as well as 3
libero.setTitle("---", for: .normal)
where libero is a uibutton
You can Use sender argument
#IBAction func TickToeButtonClick(sender: AnyObject) {
sender.setTitle("my text here", forState: .normal)
}
In Swift 4 I tried all of this previously, but runs only:
#IBAction func myButton(sender: AnyObject) {
sender.setTitle("This is example text one", for:[])
sender.setTitle("This is example text two", for: .normal)
}
Note that if you're using NSButton there is no setTitle func, instead, it's a property.
#IBOutlet weak var classToButton: NSButton!
. . .
classToButton.title = "Some Text"

Changing text of UIButton programmatically swift

Simple question here. I have a UIButton, currencySelector, and I want to programmatically change the text. Here's what I have:
currencySelector.text = "foobar"
Xcode gives me the error "Expected Declaration". What am I doing wrong, and how can I make the button's text change?
In Swift 3, 4, 5:
button.setTitle("Button Title", for: .normal)
Otherwise:
button.setTitle("Button Title", forState: UIControlState.Normal)
Also an #IBOutlet has to declared for the button.
Just a clarification for those new to Swift and iOS programming. Below line of code:
button.setTitle("myTitle", forState: UIControlState.Normal)
only applies to IBOutlets, not IBActions.
So, if your app is using a button as a function to execute some code, say playing music, and you want to change the title from Play to Pause based on a toggle variable, you need to also create an IBOutlet for that button.
If you try to use button.setTitle against an IBAction you will get an error. Its obvious once you know it, but for the noobs (we all were) this is a helpful tip.
Swift 5.0
// Standard State
myButton.setTitle("Title", for: .normal)
Swift 5:
let controlStates: Array<UIControl.State> = [.normal, .highlighted, .disabled, .selected, .focused, .application, .reserved]
for controlState in controlStates {
button.setTitle(NSLocalizedString("Title", comment: ""), for: controlState)
}
Swift 3:
Set button title:
//for normal state:
my_btn.setTitle("Button Title", for: .normal)
// For highlighted state:
my_btn.setTitle("Button Title2", for: .highlighted)
Changing title when attributed is a bit different :
I just ran into a problem : If you have an UIButton with an Attributed Title, you have to use :
my_btn.setAttributedTitle(NSAttributedString(string: my_title), for: my_state)
as, per Apple SetTitle Doc :
If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one.
I had an attributed title and I tried to setTitle on it, with no effect...
Swift 3
When you make the #IBAction:
#IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("string goes here", for: .normal)
}
This sets the sender as UIButton (instead of Any) so it targets the btnAction as a UIButton
As of 12/12/2021 - Swift version 5.5.1^ assuming you already have an IBOutlet linked to yourButton in a normal state.
yourButton.setTitle("Title of your button", for: .normal)
swift 4.2 and above
using button's IBOutlet
btnOutlet.setTitle("New Title", for: .normal)
using button's IBAction
#IBAction func btnAction(_ sender: UIButton) {
sender.setTitle("New Title", for: .normal)
}
Swift 3
let button: UIButton = UIButton()
button.frame = CGRect.init(x: view.frame.width/2, y: view.frame.height/2, width: 100, height: 100)
button.setTitle(“Title Button”, for: .normal)
To set a title for a button in Xcode using swift - 04:
first create a method called setTitle with parameter title and UIController state like below ;
func setTitle(_ title : String?, for state : UIControl.State) {
}
and recall this method in your button action method
like ;
yourButtonName.setTitle("String", for: .state)

Resources