I want my UIButton to show up the highlighted state when I click over a button that is already selected.
Basically in the highlighted state I apply a *.png image as my UIButton backgroundImage to give a pressed down effect.
But if the button is already in the Selected State When I click over it again I just can't see the highlighted state but it goes straight to the normal state!
Watch the Issue--> Video of the Issue!
Help please
//0 init UIButton
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, aSide, aSide)];
//1 Give it a backgroundColor
[button setBackgroundColor:aColor];
[..]
//2 Set titleLabel and its style
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
UIImage *shadowImage = [UIImage imageNamed:kBtnShadow];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];
[button setBackgroundImage:shadowImage forState: UIControlStateHighlighted];
[button setTitle:aLabel forState: UIControlStateNormal];
//3 Assign tag and Action
[button setTag:tag];
[button addTarget:target action:a forControlEvents:UIControlEventTouchUpInside];
The various states: UIControlStateNormal, UIControlStateSelected, and (UIControlStateSelected | UIControlStateHighlighted) are all actually distinct. If you want your shadowImage to apply both in the (only) highlighted state and in the highlighted+selected state, you must also set:
[button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)]
In swift this would be:
button.setBackgroundImage(shadowImage, forState: UIControlState.Selected.union(UIControlState.Highlighted))
In Swift v3 (Nov. 2016):
button.setBackgroundImage(shadowImage, for: UIControlState.selected.union(UIControlState.highlighted))
Swift 4.2
Applicable programmatically only.
aButton.setImage(UIImage(named: "your_image"), for: [.selected, .highlighted])
Related
I create a button like this, but when I click on it there's no changing effect.
UIButton *button = [[UIButton alloc] init];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle: #"xxxxx" forState: UIControlStateNormal];
[button setTitleColor: [UIColor colorWithRed:0.98 green:0.44 blue:0.05 alpha:1] forState: UIControlStateNormal];
I'm not going to use two images, I just want the text to change a bit. Just like a fresh button dragged to the UIViewController.
What did I miss?
You have to set a title and a title color when the button is clicked.
[button setTitle: #"clicked" forState: UIControlStateHighlighted];
[button setTitleColor: [UIColor redColor] forState: UIControlStateHighlighted];
See UIControl Class Reference
I've got an UIButton which I'm successfully setting the background image for. I want to change the background image when the user taps it (which navigates to the next view controller). Unfortunately, the background doesn't change for taps. However, it does change when I tap and hold the button so I'm pretty confused. Code looks like the following:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x_offset, y_offset, width, BUTTON_HEIGHT)];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
UIImage *buttonImage = [[UIImage imageNamed:#"main_button"]
resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
UIImage *pressedbuttonImage = [[UIImage imageNamed:#"main_button_pressed"]
resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateHighlighted];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateApplication];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateDisabled];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateReserved];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateSelected];
return button;
I know I overdid it with the UIControlStates but I wanted to demo that I've tried them all :)
The issue is you are only changing the images when each event occurs. When you tap and hold it'll show the highlighted image. When you change that state it revert back to normal state and show the image (Normal background image)
You can achieve the desired behavior in two ways:
1)
In your IBAction, change the normal button image.
- (IBAction)buttonPressed:(UIButton *)button
{
UIImage *pressedbuttonImage = [[UIImage imageNamed:#"main_button_pressed"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[button setBackgroundImage:pressedbuttonImage forState:UIControlStateNormal];
}
2)
Set selected or highlighted property of button to true
- (IBAction)buttonPressed:(UIButton *)button
{
button.selected = YES;
}
Well the issue is once you have stopped tapping the button, the button's background image is going to default to whatever background image you set for the UIControlStateNormal. So what you need to do is when your button is tapped, reset the UIControlStateNormal background image to whatever image you want after selection of the button.
I have a few buttons and when I set a picture as background the icons turn blue, for example:
[self.button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
All pictures are black and white, so I don't understand why they change their color when I set them as a backgroundimage.
Make sure to init the button as a "UIButtonTypeCustom"
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
Then if you want the whole button to be an image, you use:
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
... Or if you want to have the image as the background, and still see the "title", you use:
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
First do
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
and then set image
[self.button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
So I've subclassed a UIButton and the only method I've added to it is setTitle (I am using icon fonts so I created an Icon_UIButton for those buttons represented via icons).
I have this code:
Icon_UIButton* button = [[Icon_UIButton alloc] init];
[button setTitle:#"R"]; //R is a specific icon in my icon font
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
This looks like it works, but after I set a button to selected using:
[button setSelected:YES];
whenever I tap and hold down on that button, it turns black again.
It's like a selected Icon_UIButton ignores it's titleColor for a highlighted state when it's selected at the same time.
Has anybody experienced this?
Ok, I found the correct answer to my question in this thread:
Highlighted state for UIButton doesn't come when going from selected to normal state
I hate to answer my own question, but just in case anybody else types in search terms that might match this question in google, here is the answer (shortened, but directly from the other thread):
There's another UIControlState (or rather, a mix of control states):
UIControlStateSelected | UIControlStateHighlighted
So now the code looks like this:
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected | UIControlStateHighlighted];
I need a quick way to change the color of an UIButton live (in response to a delegate call) . So the button is drawn with a white background (standard) then when a delegate call comes it needs to change to a blue/red background.
I tried :
btn.backgroundColor = [UIColor redColor];
[btn setNeedsDisplay];
This doesn't work...
Also it would be a good ideea to change the color of the button text... How is this done????
Try using it like this :
[button setBackgroundColor:someColor forState:UIControlStateNormal];
Did it work for you .. ? I am assuming you have made your button a custom button type.
If it worked, you may want to consider, accepting the answer
First make your UIButton type Custom..
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setbackgroundColor:[UIColor blueColor] forState: UIControlStateNormal];
[myButton setTitleColor:[UIColor blueColor] forState: UIControlStateNormal];