UIButton title disappears when button disabled - ios

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;

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

keep UIButton current state when it is disabled

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

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.

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

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