Make UIBarButtonItem appear disabled? - ios

I know about self.navigationItem.rightBarButtonItem.enabled = NO, but how do I make a UIBarButtonItem appear disabled but actually detect when the user taps it? I want to do this in case I want to alert the user what is incomplete.

The way i'd do it is not disable it, but when its 'disabled' set another bar button item there with a disabled looking background and no alternate image for the tap event. Then when it is tapped, show an alert view to tell them that it isn't available:
- (void)init
{
[self setDisabledBarButtonItem:[UIBarButtonItem alloc] initWith...];
[disabledBarButtonItem addTarget:......];
[self setEnabledBarButtonItem:[UIBarButtonItem alloc] initWith...];
[enabledBarButtonItem addTarget......];
}
- (void)timeToDisableBarButtonItem:(id)sender
{
[self.navigationitem setRightBarButtonItem:disabledBarButtonItem animated:NO];
}
- (void)timeToEnableBarButtonitem:(id)sender
{
[self.navigationitem setRightBarButtonItem:enabledBarButtonItem animated:NO];
}
Good UX practises state however that you shouldn't need to tell your user that it is disabled, they should be able to tell without an alert. Easier said than done of course :)
Would love to see a cleaner solution than this, but its the only way I think your going to get it to work.
Hope that helps :)

I had a similar problem, except I was trying to make a disabled button look enabled. I found a much nicer solution that is available in iOS 5.0 - you can use setTitleTextAttributes:forState to control the appearance of your enabled (or in my case, disabled) state, as well as many other states.
API reference:
https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIBarItem_Class/Reference/Reference.html#//apple_ref/occ/instm/UIBarItem/setTitleTextAttributes:forState:
In most cases you won't want to do this. However, in my case it proved necessary. I have a toolbar button that is a document title, and pressing it while in edit mode allows brings up a UITextField to edit the title, but in run mode the title should not be editable while still looking like a title, rather than a disabled button. By changing the text color to match my enabled state, I achieved the look and behavior I wanted without having to swap the button or the action out (and consequently have to synchronize my title text across three places instead of two).

Related

Swift UIButton - How to remove underline?

I have problem when in iOS settings is enabled this setting "Button Shapes"
It causing this underline in application (first picture with enabled setting, second without)
Any idea how to programatically or in storyboard disable it?
I tried attributed text but I get same result :(
I'm newbie in Swift.
Thanks for help!
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
This is an answer by user4291543 from this question Remove underline on UIButton in iOS 7
[yourBtnHere setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal];
I found this answer works with SWFrameButton
And for all the others saying "Don't Do This", SWFrameButton is a very good example of when you would want to do this. I also think the OP's situation is a perfectly valid scenario as well...
I totally agree with #maddy's comment:
It's not a problem. You should not make any attempt to counter any accessibility changes set by the user. They are there for a reason.
But I did stumble on a way to accomplish the task at hand...
In addition to a UIButton, you'll also need to make a .png file that contains nothing (meaning the entire contents have an opacity of 0%). Go ahead and load that into your xcode project's assets.
Now go ahead and set the Button's Background to that image you just provided. (In my case, I called it clear) This will remove the underline from the button's text. However, now you can't see the boundaries of the button. This can be solved by changing the Background of the button's View. Go ahead and select any color for the View's Background property and now the background of the View visibly defines the button's boundaries. You're able to see this because your clear.png has an opacity of 0%.
see the Attributes inspector for UIButton here.
Rather than trying to defeat the underline by going to make a label perform some action via UITapGestureRecognizer, this allows you to still use a UIButton. Keeping inline with accessibility features to mark buttons for people that want to do that.
You could create a custom button class with a label (with clear color). If you set the text of this label instead it shouldn`t get an underline.
Are you sure you want to do that?
Apple added an accessibility feature to mark buttons for people that want to do that. Apple will probably reject your app because it defeats a system function meant to help the disabled.
I found the solution. All you have to do is set a picture as the background of the button. just pick a picture with the same color as the button you created.

IBAction Button Press (Don't want to see the press)

So I have a few buttons in an application and obviously they can all be pressed and do various things...
My question is, how can you "not display" the colour change or visual aid that happens on screen once a button is pressed?
To be honest, I'm not sure how to word this or which terminology to use, but basically, when a button is pressed, I don't want any change on screen to happen (apart from whats supposed to happen in the IBAction).
It sounds counter intuitive, but I was hoping for a built in function like IBActionPress.Visual = NO; or something like that...
I really hope this makes sense. Thanks in advance.
Try using
yourUIButton.adjustsImageWhenHighlighted = NO;

Adding a custom UIButton and action to a view in window app delegate (ios)

I am working on a tab bar application which needs a global title bar with a setttings button attached. I have achieved adding an imageview to the main window, then a label, another image, and finally a button. However, I cannot get this button to fire an action. it is set up like this:
[settingsButton addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchupInside];
Then the action defined like:
-(void)settings{
NSLog(#"please do something");
}
However nothing happens, the image doesn't even change like it is being pressed. Can i not define buttons in the app delegate this way? is it because the target is not something that is control based? i tried different targets but i must not fully understand what setting a target is. thanks for any help.
P.S.- i tried setting up the method like so as well:
-(void) settings:(id)sender{
}
and calling it by replacing the action with #selector(settings:) to no avail. Thank you.
I solved it. I forgot I changed the view behind the button to a UIImageView. I can't believe I didn't think of this before since this same issue cost me 2 hours the other day. When you have an imageView always remember to set this flag if you want stuff on it:
(someImageView).userInteractionEnabled = YES;
Hopefully I save someone an hour or two of needless headaches.
P.S. Thanks for the speedy comment !

iOS Can't change UIBarButtonItem image properly

I'm having a problem when I try to change a UINavigationBar's "back" button custom icon. To achieve such a thing, I'm using the following code:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* leftBtn;
UIImage* botonVolverImg = [UIImage imageNamed:#"boton_volver.png"];
leftBtn = self.navigationItem.leftBarButtonItem;
leftBtn.image = botonVolverImg;
}
But, when the view appears, you see this:
a busy cat http://www.timotteo.com.ar/boton.png
(You can see that the old button still appears at the back, plus the image I chose looks a bit streched)
I've been changing the imageInsets property, but that doesn't seem to work. I've also been reading the forum around, but couldnĀ“t find the exact solution.
Any suggestions are welcomed!
The image property for UIBarButtonItem doesn't correspond to the background image, only an image that provides additional context. If you're targeting 5.0+, your best bet would be to use -setBackButtonBackgroundImage:forState:barMetrics: to set a background for the bar button item.
To supplement Mark's answer, if your customer is requiring you to support iOS 4, you could create a UIButton that looks exactly how your customer wants it to (ie, just that image), and then use UIBarButton's initWithCustomView method to create your own back button. You can then have that button trigger popViewController or whatever appropriate action you need.

Make green color UIActionSheet button?

I know I can access a individual button on a actionSheet using a for loop to access the particular button in the action sheet I want, but the thing is, how would I make a green button?
I know each button acts almost identically to a UIButton so how should I go upon making one of my buttons in the action sheet green?
I just need some tips or help with this as it is the last part of my app that isn't done!
Thanks!
When I have needed customizations for UIActionSheets, sometimes I find methods to make it happen simply, but more often I end up having to set up a custom view on way or another. I've added all manners of custom controls with:
[sheet addSubview: myCustomActionSheetController.view];
You might need to set the size as well.

Resources