Setting an UIImage for UIControlStateHighlighted - ios

board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.

when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}

I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.

Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().

Related

when i click a button in ios app i want the image to change

the title much explains it really in my button
- (IBAction)LikeBtn:(id)sender
when the button is clicked i want ti to swap images from a small grey icon to a green icon
im trying it this way but i cant seem to get it to work
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage :#"liked.png" forState:UIControlStateSelected];
{
Has Any body any ideas how this can be done at the moment the image is selected in the xcode control panel and is named likebtn.png and need it changing to liked.png after clicked
Assuming you have the image properly added to your project, and it's a png:
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked"] forState:UIControlStateSelected];
}
PS: Are you sure you wish to set the image for the selected state? If not, use UIControlStateNormal instead.
I think you need to setImage in viewDidLoad when creating button:
[LikeBtn setImage :[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
In addition to what Maddy says about passing a UIImage rather than a string for a selected state, you will also need to declare that the button is now selected. If you change your code to this, it should work for you
-(IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
senderButton.selected = YES;
}
You can also put a if (senderButton.selected) condition within the IBAction method to detect and toggle between button states, changing the state from YES to NO.
Hope this helps

Changing button image when another button is clicked

I'm facing a problem in xcode, in one of my view controllers i have two buttons, when one of them is clicked (appleBtn) the second button's (topMenu1Btn) image should change
I created an outlet for both buttons, an action for the applebtn when clicked
- (IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView = [UIImage imageNamed:#"Apple.jpg"]
}
the thing is the imageview is read-only
is there anyway to do this?
- (IBAction)HandleBtnClick:(id)sender
{
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
}
You could try this to set the image for the button.
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you want the background image use setBackgroundImage: instead
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
Or
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you use the second method then make sure that the frame of image matches with the button
Make sure that Button type is Custom
(IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView.image = [UIImage imageNamed:#"Apple.jpg"]
}
You can't modify the imageview, but you can modify the image in it ;)

UIButton titleColor upon selection and highlight

I'm trying to set a UIButton's titleColor to a certain color when it is selected and when it is highlighted, however, I need to set the button to be selected when the user touches down on the UIButton.
I've set it up like so:
[button setTitleColor:normalColor forState:UIControlStateNormal];
[button setTitleColor:superDuperSpecialColor forState:UIControlStateHighlighted];
[button setTitleColor:superDuperSpecialColor forState:UIControlStateSelected];
[button addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventTouchDown];
But when the button gets selected in the action: method using [senderButton setSelected:YES], it sets the titleColor to normalColor, rather than superDuperSpecialColor, which it should be, as it's both highlighted AND selected.
Commenting out the setSelected: call prevents the button from becoming and staying selected and commenting out the highlighted state color doesn't have any effect, it seems.
Will I have to add targets for UIControlEventTouchCancel, UIControlEventTouchUpInside and UIControlEventTouchUpOutside in order to call setSelected: after the highlight ends -or- change the titleColor for UIControlStateNormal to superDuperSpecialColor when the button gets a touch?
On a side note, I would have liked to set the titleColor like so:
[button setTitleColor:superDuperSpecialColor
forState:(UIControlStateHighlighted | UIControlStateSelected)];
But that doesn't seem to work. Why is that? Does Objective-C check for state equivalency?
I verified your results, and it seems like a bug in iOS. It fails on both the simulator and the device (iOS 6.1). It seems like if the selected and highlighted states are both YES, then the selected settings should override the highlighted settings. It's mostly implemented that way. The button's text value works like this, but the color seems to get it wrong (defaults to normal).
You might want to try it against iOS 7 if you have the latest XCode to see if they've fixed this, otherwise report it as a bug.
Since selected isn't a commonly used state for a UIButton, it probably wasn't properly tested in combination with other states.
As a workaround, in your action method, you could set the color for the normal state to superDuperSpecialColor and add another action for the touch up events to set the normal color back to normal. Since the state while the button is pressed should never actually be normal, this won't break anything if they do fix it in the future.
For both selected and highlighted state it's also necessary to set title, while for only highlighted state title is taken from normal state. So, don't forget to add extra line:
[button setTitleColor:UIColor.blackColor forState:UIControlStateSelected];
[button setTitleColor:[UIColor.blackColor colorWithAlphaComponent:0.5f]
forState:UIControlStateSelected | UIControlStateHighlighted];
[button setTitle:#"Title" forState:UIControlStateSelected];
[button setTitle:#"Title" forState:UIControlStateSelected | UIControlStateHighlighted];
Сode below works fine without explicit setting title for highlighted state:
[button setTitleColor:UIColor.greyColor forState:UIControlStateNormal];
[button setTitleColor:[UIColor.greyColor colorWithAlphaComponent:0.5f]
forState:UIControlStateHighlighted];
[button setTitle:#"Title" forState:UIControlStateNormal];
I think you need to check your UIButton type, in creation. I have code like this and works fine.
But setting state two states in one line doesn't work for me.
[button setTitleColor:superDuperSpecialColor
forState:(UIControlStateHighlighted | UIControlStateSelected)];

UIButton setSelected: NO doesnt show default background image

I have a custom UIButton with 2 background images:
in Default State: default.png
in Selected State: selected.png
set in the Xib File.
This is gets invoked on button touch down:
-(IBAction)numberSelected:(id)sender{
NSLog(#"Button pressed %#",[sender currentTitle]);
UIButton* button = (UIButton*)sender;
button.selected = !button.selected;
[button release];
}
The wrong behavior is this:
I press the button, the background image switches to selected.png (correct), I press it again, and no background image is shown. I press it another time, the app crashes.
I think you forgot to set : UIControlStateSelected adding the image.
[button setImage:[UIImage imageNamed:#"Selected.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
Edit:
Why do you make [button release]; Of course it will crash the second time.
No need to release the button there. Remove
[button release];
[button release];
This statement has to be removed!

UIButton Background image Change programmatically

How can I change the background image of my UIButton in click event? And refresh it in a few second with the previous image? I mean change it's background image on click and reset it after the click.
[yourButton setImage:[UIImage imageNamed:#"yourNormalImage.png"] forState:UIControlStateNormal];
[yourButton setImage:[UIImage imageNamed:#"yourImageWhenClicking.png"] forState:UIControlStateHighlighted];
put your clickEvent image as highlighted image of your button..
The above answer is not working because it doesn't change the background but the side image of the UIButton, to change the background image use:
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourimage"] forState:UIControlStateNormal];

Resources