Button images loaded white? - ios

I set buttons images for normal state and some images is loaded and others appears white.
I need help how to recognize the problem ?
I set the image using the code below
button.setImage(UIImage(named: "icon"), forState: .Normal)

Just change
button.setImage(UIImage(named: "icon"), forState: .Normal)
to this
button .setBackgroundImage(image, forState: UIControlState.Normal)

Related

How can i add a animation on the button' imageview when the button' state changed in iOS?

I have a button here, and set two different images for two different state:
button.setImage(image1, for: .normal)
button.setImage(image2, for: .selected)
now i want to add an animation on the button's imageview when the button' state changed, so how can i make it? if anyone can help me ,thanks a lot!
Do it like this
button.setIMage(image1, for: .normal)
UIView.animate(withDuration: 0.5) {
button.setImage(image2, for: .selected)
}

UIslider with custom background images

My code so far in getting slider to work with custom background image
self.slider.setThumbImage(#imageLiteral(resourceName: "thumb"), for: .normal)
self.slider.setMinimumTrackImage(#imageLiteral(resourceName: "slider"), for: .normal)
self.slider.setMaximumTrackImage(#imageLiteral(resourceName: "slider2"), for: .normal)
self.slider.setValue(0.5, animated: true)
I want a slider like temperature where user scroll and it fill with color. But unable to do so. Please help
This one is not working anymore
Dashed slider

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!

Change color of text when button is pushed?

I know how to change the background color but what about the actual text? There doesn't seem to be a member on a UIButton called "color" or anything like that.
My code:
#IBAction func yellowBtnClicked(sender: UIButton) {
gameboard.image = UIImage(named: "Yellow_gb")
resultsView.image = UIImage(named: "Yellow_results")
colorsView.image = UIImage(named: "Yellow_colors")
colorsBtn.color = UIColor.brownColor() //This line has the issue
}
There is no property setter color in UIButton. use instead setTitleColor.Write this in viewWillAppear
colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Normal)
This will change title color to brown for UIControlState.Normal
To set the color of title when button is in Highlighted state use
colorsBtn.setTitleColor(UIColor.brownColor(), forState: UIControlState.Highlighted)
Swift 3.0 example:
colorsBtn.setTitleColor(UIColor .white, for: UIControlState.normal)
colorsBtn.setTitleColor(UIColor .black, for: UIControlState.highlighted)

Resources