changing the colour of Button - iphone

how can I change the button colour when ever I click that button please help me out

In the action which is executed, when you click the button just add the following line:
button.backgroundColor = [UIColor randomColor];
This gives the button a random color everytime you click it. ;-)

Related

Highlighting the selected cell in grid layout in unity

I am using a panel as grid lay out and a button prefab in that lay out.I can able to attach event for the button but i couldn't able to figure out how to highlight that button when user clicks on that button.Is there any way to change background color of that clone button when user clicks on that?
you can use onClick to call a method when the button is clicked.
public Button btn;
void Start () {
btn.onClick.AddListener(ChangeColor);
}
or just use the editor, OnClick list, just note that for this, the method you want to call should be public
if you just want to highlight the button which is currently clicked and when the user clicks on another button the other button gets highlighted and this button gets back to its normal color, then that option is simply doable in the editor, just set the highlight color of the button in the editor
but if you want the buttons to change their colors after you click on them and that change is permanent then
you can just change the color of the image property of the button, by accessing its Image property and set its color to something you want.
void changeColor(){
pButton.GetComponent<Image>().color = yourColor;
}
if you dont need to change the color of the image which is used in the button, but you want to change the Button normalcolor, first you keep your previous colors, then just set the new color to the normalcolor
I also think if you want to use this method it is better to change the highlightedColor too, because after the button is pressed it, after it goes to pressed color for a second, it will enter the highlighted state until you click on another button, so I guess it is better if you change both of the colors
public void ChangeColor()
{
ColorBlock colorBlock = btn.colors;
colorBlock.normalColor = Color.blue;
colorBlock.highlightedColor = Color.blue;
btn.colors = colorBlock;
}

Button Highlight in Xcode

I'm trying to display a second button image when the button is tapped. The second image is displaying but the button gets highlighted and it doesn't look very good. How do I get it to show the second image without highlighting it?
button.setBackgroundImage(IMAGE, forState: UIControlState.Normal)

Animate button when pressed and release

In many games and apps I have seen when you click on something, the button image change and while your finger is still on the botton and move it from the button, the image change again and the button is not clicked
In spriteKit I normaly do
gave the button a unique name
in touchesBegan I use nodeInPosition and then check the name of the node
if the names match some action is run
In ViewController
creat the button in the interface builder
creat outline and IBAction and inside of the method some action is run
if the button is made programmatically I use addTarget()
But I don't know how to animate the button when is pressed/hovered and release(return to default state) when the finger is not on button
You can set two images to the button for the highlighted and normal state like this
button.setImage(imageUnhighlighted, forState: UIControlState.Normal)
button.setImage(imageHighlighted, forState: UIControlState.Highlighted)

button inside table view cell shows highlighted state on tapping on cell

I have an issue with the background image of button which is dependent of button state. I have a button inside customized table view cell and i have set different images for button's different states. Please look into the code below.
[btnNow setImage:[UIImage imageNamed:#"now_norm.png"] forState:UIControlStateNormal];
[btnNow setImage:[UIImage imageNamed:#"now_focus.png"] forState:UIControlStateHighlighted];
Whenever i tap on the actual button this works great but if i tap on area outside of button but which is still inside that same cell then this button changes its background image from UIControlStateNormal to UIControlStateHighlighted.
If i remove background image from UIControlStateHighlighted state then this issue doesn't exists but i need pressed state of the button.
Please help me out.
Thanks in advance!
Vivek Dandage.
Try with setting the cell.selectionStyle to UITableCellSelectionStyleNone
I wonder how to comment on karim's Answer. But that should be marked as right. It is the answer to the problem. I know, because I was having the same problem and setting the selectionStyle of the cell to None was the solution.

Clear button color on text field

How do you change the clear button color of a UITextField?
You want to use rightView and rightViewMode. Use a button and add an image that is a red clear button. Then add your target and perform your logic.
rightViewRectForBounds will also return the size of the button for the rightView.