UI Button Images Sticking iOS 6 - ios

I have this method for a twitter button
-(IBAction)tweetThis:(id ) sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"TwitterButtonBlack.png"] forState:UIControlStateSelected];
[sender setSelected:YES];}
}
in the simulator when i tap the button (which I made in IB as a touch up inside) the button clicks and changes to images but it doesn't go back to the original button image so for instance the blue.png changes to black.png but stays black and doesn't go back to blue. Any way I canf ix this

If I understand correctly, you're just trying to change the button image when the button is tapped. If so, you can simply just configure the button (in viewDidLoad or interface builder) like this:
[button setImage:[UIImage imageNamed:#"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"TwitterButtonBlack.png"] forState:UIControlStateSelected];
The UIButton will handle the image switching for you.

Related

Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?

[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:]

can't change image of uibutton after pressing other buttons

i have 2 buttons that keep with the pressed image even when they are released, until the other button is pressed, then the image returns to the normal image my code is this:
- (IBAction)button1_touch:(UIButton *)sender
{
[Button2 setImage:[UIImage imageNamed:#"b2Released.png"] forState:UIControlStateNormal];
[Button1 setImage:[UIImage imageNamed:#"b1Pressed.png"] forState:UIControlStateNormal];
}
- (IBAction)button2_touch:(UIButton *)sender
{
[Button1 setImage:[UIImage imageNamed:#"b1Released.png"] forState:UIControlStateNormal];
[Button2 setImage:[UIImage imageNamed:#"b2Pressed.png"] forState:UIControlStateNormal];
}
the code above works fine when the app starts, the behavior is the expected pressing both buttons, images change correctly, but when i press other button than those two, the "Button2" once it gets the "B2Pressed.png" image, it never returns to its released image, i wrote a nslog in button1_touch and it gets printed as expected, and the Button1 gets its pressed image, but the Button2 is not getting its released image, is like the method setImage of UiButton gets broken when i press other button than those two, other buttons are independent from those two, for me this behavior has absolutely no sense at all, could it be xcode broken? i am working on xcode 7.2
EDIT:
the problem occurs when i press another button only if that button has a action selector assigned to it (UIControlEventTouchUpInside).
You can use selected state of UIButton for this purpose:
- (void)viewDidLoad {
// ...
// the place where you do buttons setup
[button1 setImage:[UIImage imageNamed:#"b1Released"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"b1Pressed"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"b2Released"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"b2Pressed"] forState:UIControlStateSelected];
}
- (IBAction)firstButtonTouch:(UIButton *)sender {
button1.selected = YES;
button2.selected = NO;
}
- (IBAction)secondButtonTouch:(UIButton *)sender {
button1.selected = NO;
button2.selected = YES;
}

Make UIButton's state change to UIControlStateSelected on TouchUp

I've noticed that if I do something like this
[self setTitle:#"Follow" forState:UIControlStateNormal];
[self setTitle:#"Following" forState:UIControlStateSelected];
The button will start off with "Follow". I press it, and when I release my finger (touch up event) it changes to "Following". That's great. However, when It's now selected and I press it, it immediately changes to "Follow" before I release my finger (on a touch down event). I want to change when I release my finger (touch up event). How do I do this? I am using UIControlStates for a UIButton's title, images, and title color.
Below is my event handler:
- (void)followButtonAction:(UIButton *)button
{
button.selected = !button.selected;
}
and I set it as so:
[self.followButton addTarget:self action:#selector(followButtonAction:) forControlEvents:UIControlEventTouchUpInside];
try to use this, This works for me as toggle button.
-(IBAction)buttonTapped:(UIButton *)sender{
if ([sender isSelected]) {
[sender setSelected:NO];
}else{
[sender setSelected:YES];
}
}
Hope this works for you too.
Turns out the solution is to add another control state specifically for the combination that is causing the problem (when selected, and then being pressed). Below is the solution:
[self setTitle:#"Follow" forState:UIControlStateNormal];
[self setTitle:#"Following" forState:UIControlStateSelected];
[self setTitle:#"Following" forState:UIControlStateSelected | UIControlStateHighlighted];

UIButton Selected State Image Freezes on Autorotation

I have a UIButton in the footer of UITableView. It has an empty checkbox as its unselected state, and a checked box as its selected state. It works perfectly in portrait, but if the view ever rotates to landscape, the button's image is frozen in whatever state it was when it rotated. The button still sends messages correctly after rotation, but the image does not change.
self.theButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.theButton setImage:checkbox
forState:UIControlStateNormal];
[self.theButton setImage:checkedBox
forState:UIControlStateSelected];
This code appears in the tableView:viewForFooterInSection: of my view controller. In the action linked to the button, I set self.theButton.selected = !self.theButton.selected;
What's going on?
You can do this in your button action
- (void)didTapCheckbox:(id)sender {
UIButton *btn =(UIButton *)sender;
if (btn.selected == YES) {
[btn setSelected:NO];
}
else{
[btn setSelected:YES];
}
}
instead of your code just do this
if(self.theButton == nil){
self.theButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.theButton setImage:checkbox
forState:UIControlStateNormal];
[self.theButton setImage:checkedBox
forState:UIControlStateSelected];
}

UIButton - On touch change image

When I touch the button at that time I want to change image & when i release the touch button image is as it is.
I want to apply below code but it's not with my expectation.
please give me any suggestion.....
-(IBAction)actionEnter:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateSelected];
[sender setSelected:YES];
}
You can use UIControlStateHighlighted for this.
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateHighlighted];
You can also set this from interface builder by setting the image for highlighted state.
I think this should do it. Set the images after creating the button
[yourButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
and do this
- (IBAction)actionEnter:(id)sender{
UIButton *button = (UIButton *)sender;
button.selected = !button.selected;
}
In Swift:
button.setImage(UIImage(named: "enter.png"), forState: [.Selected, .Highlighted])
I think, you could set the image in the beginning for normal and selected state ..
Try with below when you create the UIButton object. [Use the images as per your requirement]
[myButton setImage:[UIImage imageNamed:#"enter.png"]
forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"enter-hover.png"]
forState:UIControlStateSelected];
#7KV7 got me thinking. I have favorite and ignore buttons that I want to use to mark favorite pictures and pictures that I never want to see again. I used his method to initialize the buttons and then slightly modified his method to toggle the buttons on and off.
In this example, if you mark a picture as a favorite, you want to turn off the ignore button and vice versa. The delegate handles the database stuff.
self.favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.ignoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite-Selected"]
forState:UIControlStateSelected];
[self.favoriteButton setImage:[UIImage imageNamed:#"Favorite"]
forState:UIControlStateNormal];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore-Selected"]
forState:UIControlStateSelected];
[self.ignoreButton setImage:[UIImage imageNamed:#"Ignore"]
forState:UIControlStateNormal];
If you are just toggling a button on or off, you won’t need to make it a property, since the buttonPressed sender knows which button has been pressed. I need to have them be property since I need to tell the opposite button to turn its highlight off.
- (void)favoriteIgnore:(UIButton *)buttonPressed {
// Toggle the tapped button
buttonPressed.selected = ( buttonPressed.selected) ? NO : YES;
id <ScoringToolbarDelegate> TB_delegate = _delegate;
// Turn off the other button and call the delegate
if ([buttonPressed.currentTitle isEqualToString:#"favorite"]) {
self.ignoreButton.selected = NO;
[TB_delegate favoriteButtonPressed];
} else {
self.favoriteButton.selected = NO;
[TB_delegate ignoreButtonPressed];
}
}
to change the image immediately use the backgroundImage property.

Resources