UIButton changes states when being pressed - ios

I have an UIButton configured with two states, default and selected, for different text and background, the set up is done on Interface Builder. The weird thing is when the button is in selected state, and the button is pressed, it automatically changes to default state. After the button is released, it changes back to selected state. Is there way to disable roll-back-to-default behavior?
Forgot to mention that, the type is custom.
Update
tried the following code in viewDidLoad
[self.button setTitle:#"default" forState:UIControlStateNormal];
[self.button setTitle:#"selected" forState:UIControlStateSelected];
[self.button setTitle:#"highlighted" forState:UIControlStateHighlighted];
[self.button setSelected:YES];
The button shows default when pressed.

Selected state and Highlighted state are not mutually exclusive. When you press a selected UIButton, it gets into a selected AND highlighted state.
I don't think there's a way to set that in the IB, but you can do this in code:
[button setImage:[UIImage imageNamed:#"some_image"] forState:UIControlStateSelected | UIControlStateHighlighted];

Related

Button doesn't stay pressed when in Collection View

I have an app with this code
_pushButton = [UIButton buttonWithType:UIButtonTypeCustom];
_pushButton.frame = CGRectMake(15, 2, 74, 74);
[_pushButton addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchDown];
[_pushButton addTarget:self
action:#selector(buttonReleased:)
forControlEvents:UIControlEventTouchUpInside];
[_pushButton setBackgroundImage:[UIImage imageNamed:#"1.png"] forState:UIControlStateNormal];
[_pushButton setBackgroundImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateSelected];
The behavior is that when the button is pressed the image gets darker and the button stays in that state until the button is released. The buttonPushed is called when the button is pressed and the buttonReleased is called when it is released.
That is what I expected.
I have another application with exactly the same code, that I have copied from the first.
The behavior is different: when the button is pushed, the image gets darker but about a second later the button gets to its original state even if it is still pressed. Only the buttonPushed is called.
This is not what I need.
1) Why the two apps has a different behavior?
2) How can configure the button in the second application to have the same behavior of the first?
UPDATE
In the first app the button is created in a class which subclass
UIView
In the second app the button is created in a class which
subclass UIViewController
UPDATE 2
The button which doesn't stay pressed is in a subview of a UICollectionViewCell. Apparently, buttons have a different behaviour when in a cell of a collection view.

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!

Setting an UIImage for UIControlStateHighlighted

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().

ios horizontal scroll menu - background buttons select

I try to make an horizontal navigation menu in scrollView. For every items in the menu i had a different image. When i select an item, i would like to change background image of this item, but i don't know why my code doesn't work:
for (int i=0; i<12; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *normal=[UIImage imageNamed:#"icon1"];
UIImage *selec=[UIImage imageNamed:#"icon1down"];
[btn setFrame:CGRectMake(i*larg, 0, 42.0, 43.0)];
[btn setBackgroundImage:normal forState:UIControlStateNormal];
[btn setBackgroundImage:selec forState:UIControlStateSelected];
[scrollCat addSubview:btn];
cx+=42.0;
}
[scrollCat setContentSize:CGSizeMake(cx, 43)];
Is there a bad thing in this code?
Try UIControlStateHighlighted instead of UIControlStateSelected.
Highlighted is the state when the user touches your button.
Selected is only used for some controls, like segments ; a custom button (one that say, stays selected) would/could make use of Selected.
From the UIControl_Class docs at developer.apple.com (emphasis mine):
UIControlStateHighlighted
Highlighted state of a control. A control enters this state when a
touch enters and exits during tracking and when there is a touch up
event. ...
UIControlStateSelected
Selected state of a control. For many controls, this state has no
effect on behavior or appearance. But other subclasses (for example,
the UISegmentedControl class) may have different appearance depending
on their selected state. ...

Resources