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.
Related
[myButton setImage:[UIImage imageNamed:#"btn_normal"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted"] forState:UIControlStateHighlighted];
[myButton setImage:[UIImage imageNamed:#"btn_highlighted"] forState:UIControlStateSelected];
myButton.showsTouchWhenHighlighted = YES;
I have tried this code and it is not working.
You should use the setBackgroundImage:forState. The setImage:forState changes the image which is on the same depth level as the button title. Additionally the backgroundImage is automatically fit to the button's view bounds. The image is not.
Implement onTouchDown action of that button and set button background image in that function by [btn setBackgroundImage: forState:]
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];
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])
In my application I have created 20 buttons with in a scroll view, now problem is that I was not able to highlight the selected button.
My intention is to show the pressed button with a different look than normal. When another button is pressed the previous one should become normal:
UIButton *Abutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[Abutton setTag:i-1];
Abutton.frame = CGRectMake(30.0, 0+j, 40.0, 40.0);
[Abutton setTitle:#"" forState:UIControlStateNormal];
Abutton.backgroundColor = [UIColor clearColor];
[Abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"image1.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"image2.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[Abutton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[Abutton addTarget:self action:#selector(buttonpressed:) forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:Abutton];
Finally I created the method for Abutton pressed as below:
-(IBAction)buttonpressed:(id)sender{
Abutton.highlighted=YES;
//.....
//.....
}
If do it like this then only the last button created dynamically gets highlighted. That's not exactly what I wanted.
I think you should replace your current code for the button pressed:
-(IBAction)buttonpressed:(id)sender{
UIButton *b = (UIButton *)sender;
b.highlighted = YES;
//.....
//.....
}
In your example you are specifically highlighting "AButton". This code highlights the button being pressed.
Solution 1:
Create an NSSet with references to all the buttons. In your buttonPressed method, call makeObjectsPerformSelector on the NSSet's buttons, setting them to the unhighlighted state.
Solution 2:
Use a UISegmentedControl. That seems like what you should be doing in this case anyway.
In Xcode, how do you set the background of a UIButton as an image? Or, how can you set a background gradient in the UIButton?
Complete code:
+ (UIButton *)buttonWithTitle:(NSString *)title
target:(id)target
selector:(SEL)selector
frame:(CGRect)frame
image:(UIImage *)image
imagePressed:(UIImage *)imagePressed
darkTextColor:(BOOL)darkTextColor
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = [UIColor clearColor];
return button;
}
UIImage *buttonBackground = UIImage imageNamed:#"whiteButton.png";
UIImage *buttonBackgroundPressed = UIImage imageNamed:#"blueButton.png";
CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);
UIButton *button = [FinishedStatsView buttonWithTitle:title
target:target
selector:action
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed
darkTextColor:YES];
[self addSubview:button];
To set an image:
UIImage *buttonImage = [UIImage imageNamed:#"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];
To remove an image:
[button setBackgroundImage:nil forState:UIControlStateNormal];
This will work
UIImage *buttonImage = [UIImage imageNamed:#"imageName.png"];
[btn setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:btn];
In case it helps anyone setBackgroundImage didn't work for me, but setImage did
You can set an background image without any code!
Just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want!
Make sure you have the picture you want in the supporting files to the left.
Swift
Set the button image like this:
let myImage = UIImage(named: "myImageName")
myButton.setImage(myImage , forState: UIControlState.Normal)
where myImageName is the name of your image in your asset catalog.
When setting an image in a tableViewCell or collectionViewCell, this worked for me:
Place the following code in your cellForRowAtIndexPath or cellForItemAtIndexPath
// Obtain pointer to cell. Answer assumes that you've done this, but here for completeness.
CheeseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cheeseCell" forIndexPath:indexPath];
// Grab the image from document library and set it to the cell.
UIImage *myCheese = [UIImage imageNamed:#"swissCheese.png"];
[cell.cheeseThumbnail setImage:myCheese forState:UIControlStateNormal];
NOTE: xCode seemed to get hung up on this for me. I had to restart both xCode and the Simulator, it worked properly.
This assumes that you've got cheeseThumbnail set up as an IBOutlet... and some other stuff... hopefully you're familiar enough with table/collection views and can fit this in.
Hope this helps.
In one line we can set image with this code
[buttonName setBackgroundImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
Code for background image of a Button in Swift 3.0
buttonName.setBackgroundImage(UIImage(named: "facebook.png"), for: .normal)
Hope this will help someone.