keep UIButton current state when it is disabled - ios

Hi i am making a custom UIButton which have images for UIControlStateNormal and UIControlStateSelected. But when i set [myBtn setEnabled:No]; it changes back to UIControlStateNormal state instead of disabling at selected state. How can i keep the current state of button and disable it?

Use userInteractionEnabled = NO instead of enable disable.

try to set like this,i hope this will helps you
[buttMR setBackgroundImage:yourButton.currentBackgroundImage forState:UIControlStateDisabled];

Set an image for the disabled state as well : UIControlStateDisabled
[myButton setImage:disabledImage forState:UIControlStateDisabled];

Related

UIButton title disappears when button disabled

I have a system UIButton, when I do this
self.abutton.enabled = NO;
The button’s title disappears, I do not want it to disappear I just want it to be non clickable. What should I do?
yourButton.userInteractionEnabled = NO;
OR
yourButton.enabled = NO;
[yourButton setTitle:#"ButtonDisabled" forState:UIControlStateDisabled];
Check this Apple's documentation
List of States:
UIControlStateNormal
UIControlStateHighlighted
UIControlStateDisabled
UIControlStateSelected
UIControlStateApplication
UIControlStateReserved
Have you add title for disabled state of your button e.g -
[YOUR_BTN setTitle:#"xyz" forState:UIControlStateDisabled];
If not than add it
As per my understandings you can use following code. Hope this will help you.
self.aButton.userInteractionEnabled = NO;

UIButton changes states when being pressed

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];

How to set an invisible title on UIButton?

I'm working on an Ipad application, and i need to set a title on a 'UIButton', in order to could use :
-(void)MyMethod:(id)sender
{
UIButton *resultButton = (UIButton *)sender;
if ([resultButton.currentTitle isEqualToString:#"TitleName"])
{
...
}
}
But when I use this sort of code :
[MyUibutton setTitle:#"TitleName" forState:UIControlStateNormal];
It appears "TitleName" behind the image of the 'UIButton'...
So, is it possible to fix it ?
Thanks !
Don't use the button title, it's for display, not a flag. Instead use a tag or objc_setAssociatedObject Or store the button in a dictionary and compare.
theButton.titleLabel.alpha = 0;
If you set the image of the button, it will be at the top of the button, so the button's title won't be visible. Set the button's background and it will solve your problem !
[yourbutton setBackgroundImage:YOURIMAGE forState:UIControlStateNormal];
You can do [button setTitle:title forState:UIControlStateDisabled]; as long as you know you are not ever going to disable the button. Other than that, using the tag property is probably the easiest.

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)];

How to disable the highlight control state of a UIButton?

I've got a UIButton that, when selected, shouldn't change state when being touched.
The default behaviour is for it to be in UIControlStateHighlighted while being touched, and this is making me angry.
Suggestions?
Your button must have its buttonType set to Custom.
In IB you can uncheck "Highlight adjusts image".
Programmatically you can use theButton.adjustsImageWhenHighlighted = NO;
Similar options are available for the "disabled" state as well.
In addition to above answer of unchecking "highlight adjusts image" in IB, make sure that button type is set CUSTOM.
This will work for you:
[button setBackgroundImage:[UIImage imageNamed:#"button_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"button_image_selected"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"button_image_selected"] forState:UIControlStateSelected | UIControlStateHighlighted];
3rd line is the trick here...
This works the same for setting image/backgroundImage
adjustsImageWhenHighlighted = NO;
button.adjustsImageWhenDisabled = NO;
is equally useful for having your own appearance of a disabled button.
Depending on what changes from the default to the highlighted state of the button, you can call a couple of methods to set them to what you need. So if the image changes you can do
[myButton setImage:[myButton imageForState:UIControlStateNormal] forState:UIControlStateHighlighted];
If the text changes you can do
[myButton setTitle:[myButton titleForState:UIControlStateNormal] forState:UIControlStateHighlighted];
other similar functions:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state
For Swifty Developer -
yourButton.adjustsImageWhenHighlighted = false
Swift 3+
button.adjustsImageWhenHighlighted = false
button.adjustsImageWhenDisabled = false
OK here's an easy solution if this works for you, after a week of banging my head on this it finally occurred to me to just set highlighted=NO for the 1st line of the IBAction method for the TouchUpInside or TouchDown, or whatever works. For me it was fine on the TouchUpInside.
-(IBAction)selfDismiss:(id)sender {
self.btnImage.highlighted = NO;
NSLog(#"selfDismiss");
etc, etc, etc.
}
make your button Type - "Custom"
and Uncheck - Highlighted Adjust image and
you are done.
just two things:
UIButton *btnTransparentComponent = [UIButton buttonWithType:UIButtonTypeCustom];
btnTransparentComponent.adjustsImageWhenHighlighted = NO;
I had a similar issue and found that "unchecking" Clears Graphic Content in interface builder fixed my issue
After the introduction of Style, you have to set the style to Default in IB along with setting the type to Custom to be able to disable the highlighting effect completely. Otherwise your button text will keep highlighting.
*Setting the Style to Default resets the text color to white.
avoid to set UIButton's Line Break to Clip, use instead the standard Truncate Middle

Resources