the .selected and .highlighted properties don't cut it, because for some reason the button looks even more greyed out (darker shade of the non-highlighted image) when highlighted and selected are set to YES.
I need to make my button go off, just as if the user made it go off.
How do I do that?
I now think I understand what you mean. I put a image in my UIButton and tried to change the state of the button on touch down.
- (IBAction)touchDown:(id)sender {
[(UIButton *)sender setHighlighted:FALSE];
[(UIButton *)sender setSelected:FALSE];
}
I noticed that the image does not become darker until you move your finger. If you connect an action to "touch drag inside" and check .highlighted you should see that it has turned TRUE again. You could set it back to FALSE:
- (IBAction)touchMove:(id)sender
{
[(UIButton *)sender setSelected:FALSE];
}
However
If you're only looking for a way to stop the image from turning grey when the user presses it, do this:
button.adjustsImageWhenHighlighted = FALSE;
Setting an image for UIControlStateHighlighted would also remove the greying.
UIImage *image = [UIImage imageNamed:#"img"];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
If by "go off" you mean you want to disable the button, you can use:
[myButton setEnabled:NO];
Related
Since there's no UISwitch in tvOS, I'm using a UIButton to implement a simple On/Off toggle. I've set the button title text for UIControlStateNormal and UIControlStateSelected to indicate the button's on/off state, but the new UIControlStateFocused now interferes with this by setting the title text to be the same as the default state whenever the button is in focus. This means that when the button is "On", whenever it gets focus its title changes to "Off".
The only way I've found to get around it is to explicitly set the title for the focused state in the button handler as shown below.
- (void)viewDidLoad
{
[super viewDidLoad];
// in reality these strings are setup in the storyboard
[self.enabledButton setTitle:#"Off" forState:UIControlStateNormal];
[self.enabledButton setTitle:#"On" forState:UIControlStateSelected];
// ensure the text shows up in focused state
[self.enabledButton setTitleColor:[UIColor blackColor] forState:UIControlStateFocused];
}
- (IBAction)toggleStateForEnabledButton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setTitle:[button titleForState:UIControlStateSelected] forState:UIControlStateFocused];
}
else
{
[button setTitle:[button titleForState:UIControlStateNormal] forState:UIControlStateFocused];
}
}
This feels very hackish to me, esp. since there might be lot of this going on in UISwitch's absence. Is there a better way?
Have you tried setting a title for UIControlStateFocused | UIControlStateSelected ? It's a bit field, so should be possible to combine them.
I am setting the background image on a UIButton which has title text. This works fine, except that about 5% of the time, rarely and non-deterministically, with the same code, the text does not appear.
In my UIButton subclass:
[self setBackgroundImage:normalImage forState:UIControlStateNormal];
// Also set a background image for all other states.
In a UIButton category, I have this convenience method for setting the title:
- (void)setNormalTitle:(NSString *)title {
[self setTitle:title forState:UIControlStateNormal];
[self setTitle:title forState:UIControlStateSelected];
[self setTitle:title forState:UIControlStateHighlighted];
[self setTitle:title forState:UIControlStateSelected | UIControlStateHighlighted];
}
I'm not finding this issue having been encountered by anyone. Any ideas? I can always create a custom UIView which contains the button and draws text on top using a UILabel, but I wonder if there's a known issue I'm encountering here.
Update: I seem to have worked around the issue by setting the title asynchronously:
_tabCell = loadTopNibObject(#"MYTabSmall3HeaderCell");
dispatch_async(dispatch_get_main_queue(), ^{
_tabCell.tab0Button.normalTitle = #"Option 1";
_tabCell.tab1Button.normalTitle = #"Option 2";
_tabCell.tab2Button.normalTitle = #"Option 3";
});
Update 2: Nope, the above doesn't work. I've breakpointed and printed out the empty button's text, which reports the correct string even though the button appears blank. I've also tried running setNeedsDisplay: when the button is touched, to no avail. Also tried re-setting the button text to some specific string (e.g. #"test") on touch but this doesn't kick in any visible letting.
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'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.
Hi I want to change the background of my button after I press the button i.e. the button should not change back to the previous image.
However, I am not able to do so. My code as follows. Can anyone advise how I can change the image to "custom_button_highlight" after I click on the button?
UIImage *buttonImageNormal = [UIImage imageNamed:#"custom_button"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"custom_button_highlight"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateHighlighted];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateSelected];
METHOD1
I see that you are using code to generate UIButton. It's easier to use Interface builder. Try this - In Interface builder (Xcode4), create the button & open the right sidebar. After that select the button, & the type of button as custom. After that in state config, there are various states, for each state you can set an image in image. Hope this helps...
METHOD2
If you want to use code to generate UIButton (it beats me why) then you seem to be doing the right thing. Are you sure the images are there in your bundle? You did not include the image extension (like .jpg etc.)
You mentioned, that you don't want the image to change back...
then instead of setting it for the states highlighted or selected, you have to set it for the state UIControlStateNormal.
But since you need it after it's been pressed, you have to catch the event that your button has been pressed and set the background image in there.
Hope it helped ;)
[_buttonConfirm setImage:[UIImage imageNamed:#"s.png"] forState:UIControlStateSelected ];
... does what you want programatically, not the background image. But I think you should follow Srikar's answer and use interface builder if you can.