iOS Can't change UIBarButtonItem image properly - ios

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.

Related

Why my Bar Button item isn't getting hidden?

I'm using UIBarButtonItem in my project. I've tried to hide the UIBarButtonItem in iOS 6.1, but I was unable to do the same using the following code:
barbuttonname.tintColor = [UIColor clearColor];
barbuttonname.enabled = NO;
This code hides the UIBarButtonItem in iOS 7.1, but in iOS 6.0 it shows the UIBarButtonItem. How can this issue be fixed?
The reason is because use of tintColor for that purpose only became available in iOS 7. in iOS 6, the buttons also typically have borders and backgrounds and each bit is handled separately. In terms of what you're actually trying to accomplish here, I think you should go about it a different way.
Instead of modifying properties on the button to hide it, simply remove it from the navigation bar or wherever you have it. For example, if it is the right button on a UINavigationBar, you would just do:
myNavigationBar.rightBarButtonItem = nil;
Then, when you want to show it again
myNavigationBar.rightBarButtonItem = myButtonItem;

How do I change an Image when a button is pressed?

Now I know this question may sound like a duplicate, but I doubt it seeing as I've researched and none of the possible duplicates answer my question. If it is a duplicate and it's been answered by all means show me the way!
But my question doesn't deal so much with a button changing an image, more so the button is the image.
So my button is an image and when pressed a sound plays and then the image changes until the sound is done. My problem is I can't get the image to change.
Now I have tried this method thinking that I could press my button and my new image would cover up my button until the sound was done:
UIImage *dolDerp = [UIImage imageNamed:#"dolphin2.png"];
[imageview setImage:dolDerp];
I have an outlet set up and it's connected to an image view object and then the action is connected to the button, so in theory when the button is pressed the new image should take over the screen. Now obviously the code needs to be tweaked so the button would go away after a few seconds when the sound is played, but my problem is I can't even get the button to display. I also would prefer to just have the button objects image change if possible?
If anyone could offer some help it's much appreciated!
the best thing is in Interface Builder assign the two images to the button Normal/Selected states.
in code just use this line:
myButton.selected = YES;
//or
myButton.selected = NO;
What #Mahmoud Fayez suggested is good. In code, the proper way to set a buttons image/background image is [button setImage:image forState:UIControlStateNormal] or the similar method for setBackgroundImage
If you are using IB though, it is indeed best to set the different images for different states in IB, then change the buttons state (which will then change the buttons image) in code.

Custom UIBarButtonItem/UISegmentedControl for UINavigationController

Currently I would like to make my UINavigationController have the look and feel like facebook's main view.
I was thinking about using a UIsegmentedControl which would have a transparent background with an image inputted into the button's view.
Another idea was creating custom buttons with their background image as an image.
If anyone has any ideas on how this is done or an example project I can refer to, please REFER me!!! Haha.
Thank you stackoverflow!!!
in this case i would just use standard UIBarButtonItems for the left and right ones.
The center would then be a custom view with 3 buttons inside.
UINavigationItem has a property called 'titleView'.
So in some appropriate controller just do s.th. like:
self.navigationItem.titleView = myCustomViewWithThreeButtonsAsSubviews;
self.navigationItem.rightBarButtonItem = myLeftUIBarButtonItem;
self.navigationItem.leftBarButtonItem = myRightUIBarButtonItem;
setting the styles of the barButtontem to UIBarButtonItemStylePlain and providing images will do the job easily.

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 !

Make UIBarButtonItem appear disabled?

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

Resources