I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.
Related
I want to add a borderless UIButton to my view. Using the interface builder, I do this by dragging a Round Rect Button from the object library. Then, on the attributes inspector, change the Type to Custom and leave the title to "Button." Everything is okay with the interface builder. But, this is not the case with the programmatic approach. Programmatically, this is how I do it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
When I run the app, the button is not displayed. Maybe I'm missing something here but when I change the type to UIButtonTypeRoundRect the button is displayed. But, again, I want the button to be borderless.
Anyway, I can always use the interface builder. However, I want to understand why the programmatic approach doesn't work. So, does anyone know what the problem is?
UIButtonTypeCustom is really, actually a custom button type. There are no meaningful values setup for it by default. If you want to display it, you have to set its background color and/or its title color to a non-transparent one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];
Here i have used your code the Thing you missing is that
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Hi I was in the same situation.
I do not know why somewhere in my code there was
button.translatesAutoresizingMaskIntoConstraints = NO;
Once I've deleted that line I was able to set my button frame without any issues.
Hope this helps
I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that?
I tried to put into the action method of the button something like this:
snapshotCheckbox.image = [UIImage imageNamed:#"snapshot.png"];
but i guess this method is for UImageViews. How can i do the same thing for a button?
Thank you very much for reading my post :D
you can set the image for a given state of the button in the viewDidLoad:
[myButton setBackgroundImage:[UIImage imageNamed:#"myBackgroundImage.png"] forState:UIControlStateHighlighted];
A button has two properties image and backgroundImage..
For setting image use
button.currentImage = image (or)
[button setImage:image ForState:UIControlStateNormal];
For setting backgroundImage use
button.currentBackgroundImage = image (or)
[button setBackgroundImage:image ForState:UIControlStateNormal];
First set the tybe of the button to
button = [UIButton buttonWithType :UIButtonTypeCustom];
then use
[button setImage:[UIImage imageNamed:#"imagename.type"] ForState:UIControlStateNormal];
One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom".
This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.
Hope this helps.
You have the setBackgroundImage:forState: method on the button object. See Setting an image for a UIButton in code for more information (seconds answer).
Also, UIButtons automatically change the image when pressed if you set an image for the UIControlStateHighlighted state (though only as long as the user keeps pressing on the button).
Setbutton
if u need image while clicking
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateHighlighted];
or if u want image after Selecting it
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateSelected];
and on that onclick function mention
as
snapshotCheckbox.Selected=YES;
To set an image on a button, 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.
I have custom button, with an image that I'd like to shift down within the button. I don't want to mess with the clickable portion of the button, but just want the image moved. I've tried adjusting button.imageview.frame, but it didn't seem to do anything.
Here's my code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, 25.0, 25.0);
UIImage *buttonOnImage = [#"tabbar_icon_0"];
[button setImage:buttonOnImage forState:UIControlStateHighlighted];
[button setImage:buttonOnImage forState:UIControlStateSelected];
UIButton has imageEdgeInsets property, which can be used to shift image inside the button.
In most cases it helps a lot. You can access it from IB (in Edge drop down choose Image and then change Inset fields), so you can try different values and see if it works for your situation.
Could you not adjust the image's size by padding it out with transparency in Photoshop?
I'm trying to change the background color of a button when it's selected and don't want to use an image.
[mBtn setBackgroundColor:[UIColor grayColor] forState:UIControlStateHighlighted];
Any thoughts?
I'm replying to this old thread because it pops up consistently in searches for a solution to this problem and I have seen no solution elsewhere. It is truly annoying that setTintColor only applies to the highlighted state of a UIButton. Six months ago, it was equally annoying that it applied only to iOS 5, but that will hopefully be less of an issue going forward. With that in mind, I've drawn upon and combined a number of community suggestions to composite a general purpose solution to tinting a group of buttons in their normal state.
The method below accepts an NSArray of UIButtons and a set of color specifications as input. It applies the color specifications to one button using setTintColor, renders the result to a UIImage, and applies that image as the background image of the entire set of buttons. This avoids the need to create discrete image files for button colors. Also, it does so using a stretchable image so that it may work with a collection of buttons of different sizes (though note that it assumes the default corner rounding factors of UIButton). I hope you'll find it useful for iOS 5 targets.
- (void) setColorOfButtons:(NSArray*)buttons red:(float)red green:(float)green blue:(float)blue alpha:(float)alpha {
if (buttons.count == 0) {
return;
}
// get the first button
NSEnumerator* buttonEnum = [buttons objectEnumerator];
UIButton* button = (UIButton*)[buttonEnum nextObject];
// set the button's highlight color
[button setTintColor:[UIColor colorWithRed:red/255.9999f green:green/255.9999f blue:blue/255.9999f alpha:alpha]];
// clear any existing background image
[button setBackgroundImage:nil forState:UIControlStateNormal];
// place the button into highlighted state with no title
BOOL wasHighlighted = button.highlighted;
NSString* savedTitle = [button titleForState:UIControlStateNormal];
[button setTitle:nil forState:UIControlStateNormal];
[button setHighlighted:YES];
// render the highlighted state of the button into an image
UIGraphicsBeginImageContext(button.layer.frame.size);
CGContextRef graphicsContext = UIGraphicsGetCurrentContext();
[button.layer renderInContext:graphicsContext];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIImage* stretchableImage = [image stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIGraphicsEndImageContext();
// restore the button's state and title
[button setHighlighted:wasHighlighted];
[button setTitle:savedTitle forState:UIControlStateNormal];
// set background image of all buttons
do {
[button setBackgroundImage:stretchableImage forState:UIControlStateNormal];
} while (button = (UIButton*)[buttonEnum nextObject]);
}
[mBtn setTintColor:[UIColor grayColor]];
This only effects the highlighted state, so I believe that this is what you're looking for.
You can also set it from Interface Builder, from the Highlight Tint drop-down menu.
Just for people that will land here like I did when searching for changing background colors for highlighted state...
I ended up with an UIButton subclass that has a property for backgroundHighlightColor and tracks highlighting through KVO. Here's the link to GitHub: SOHighlightButton
You should be able to adapt it to any other scenario if you need more / other properties ot the UIButton to change if highlighted.
There is no method like this, setBackgroundColor: forState:
Check documentation. you need to use image.
The following code basically does work, only the titleLabel is not shown when the result is displayed.
buttonRectis just a CGRect containing the buttons frame. I don't think the cause of the problem is there, because the button is displayed at the position he should be at.
[contactButton.titleLabel center] did not help, bringing the subview titleLabel to front didn't either.
UIButton *contactButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
contactButton.frame = buttonRect;
contactButton.titleLabel.text = #"Contact Someone!";
[contactButton addTarget:self action:#selector(showContactWindow) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarController.view addSubview:contactButton];
I really have no idea what could be causing this.
[myButton setTitle:#"Contact Someone!" forState:UIControlStateNormal];