UIBarButtonItemStyleDone background image using the iOS 5 appearance API - ios5

How can I provide a visually distinct bar button item background image for bar buttons with the UIBarButtonItemStyleDone style and the editing state of the Edit/Done button? None of the documented UIControlState values in the UIBarButtonItem appearance proxy's setBackgroundImage:forState:barMetrics: seem to do the trick.

I'm not sure why this was never answered but if you are still looking then simply create a property for a BarButtonItem in your .h, assign it in IB, then set the background property of that barButtonItem. Works fine for me and never "changes back to default appearance".
- (void)viewDidLoad
{
[super viewDidLoad];
[self.barButton setBackgroundImage:[UIImage imageNamed:#"YOURIMAGE"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}

I have just found and posted a possible solution to this problem.
You can 'inject' custom styles using a UIBarButtonItem category:
Customizing UIBarButtonItem "Done" style and "Plain" style separately using UIAppearance

Related

Set UIToolbarPosition for created UIToolbar

I'm writing app which is aimed only for iOS5 devices, so I'm trying to maximize usage of new appearance API.
I can change background of my UIToolbar with following method:
[[UIToolbar appearance] setBackgroundImage:<myImage> forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
Everything works fine, but I'm looking for more customization, so I was wondering about usage of UIToolbarPosition and I run into some problems. Using InterfaceBuilder or adding UIToolbar programmatically I'm positioning it at the top of main view - so I'm expecting UIToolbarPosition to be set as UIToolbarPositionTop, but from what I'm testing it doesn't work automaticly nor can I find any API which allows me to set UIToolbarPosition (Yes, I googled it already).
So - main question - how to create UIToolbar and tag it properly, so it can response only to UIToolbarPositionTop or UIToolbarPositionBottom, so I can use:
[[UIToolbar appearance] setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBackgroundImage:<myImage_2> forToolbarPosition:UIToolbarPositionBottom barMetrics:UIBarMetricsDefault];
EDIT: More details, as asked.
I have multiply toolbars in my application - for instance, toolbars at top of the screen on 2 screens, toolbars acting as accessory views for keyboard and 2 toolbars at the bottom of modals screens. I'd like to maximize usage of new appearance API instead of customizing toolbars on each screens, hence I'm asking about whole UIToolbarPosition thing and how to use it.
I know I can achieve what I want just by customizing each UIToolbar separately, but I'm just curious about new API and UIToolbarPosition usage.
UIToolbarPostion isn't a property that you're supposed to be setting programmatically - instead, it allows you to tell the program how to handle a toolbar when it's in different positions. This is mostly for toolbars that are getting pushed around by screen changes (autorotation) or are on something like a navigation controller that has variable content.
That being said, if you want to directly access your toolbars so that you can use them/set properties/etc there are a couple of methods. It sounds like you know what tagging is, and this is a valid method - just give the toolbar a tag in IB or programmatically (either edit the tag property in the side bar for IB or use the .tag property when you declare the toolbar). Then you can use the viewWithTag method to access your toolbar. However, a better method would be to just create an IB property for the toolbar (same as with labels or buttons) by control-dragging over to the header file from the toolbar. Then you could just write [nameOfToolbarProperty doSomeMethod]. If you're creating your toolbar with code then just make a reference to it the same way e.g.
UIToolbar *tref = [/*toolbar creation code*/];
In conclusion, your code
[[UIToolbar appearance] setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
could be made to work by adding
//Connect this to your toolbar in Interface Builder
#property (weak, nonatomic) IBOutlet UIToolbar *tref;
to your header. Then just do
[tref setBackgroundImage:<myImage_1> forToolbarPosition:UIToolbarPositionTop barMetrics:UIBarMetricsDefault];
Note that this just tells the program what image to display if the toolbar is in the top position - it does not set the position of the toolbar. UIToolbarPosition is a constant (so you cannot set it).

Change (not init) a UIBarButtonItem identifier programmatically?

In IB I can set the identifier of a UIBarButtonItem to 'play' which adds an image of a play button (right-pointing triangle).
Is there a way to change this image programmatically? I want to change it to 'pause' when the play button is pressed.
I know you can initialize a UIBarButtonItem with an identifier but I've yet to find a way to change it after it's been initialized. Is this even possible?
The only thing I can think of is to remove the old button and initialize a new one in its place, but this hardly seems efficient.
Any thoughts?
Ok I've googled this question to death and ran into sample code from Apple where they do exactly the same thing (toggle play/pause button graphic on a toolbar button). But instead of using the built in play and pause identifiers of UIBarButtonItem they use a custom UIButton and toggle custom images.
So if Apple goes through the trouble of creating and toggling custom images on a UIButton instead of the built in play and pause UIBarButtonItem buttons then I think it's pretty safe to say there's no way to programatically change the identifier of a UIBarButtonItem.
This is what they (Apple) do to toggle the images when the button is pressed:
// Call this when the button you want to toggle is pressed:
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
Replace p.playing with whatever BOOL you want to hold the state of your button. playButton is the custom UIButton in the toolbar. pauseBtnBG and playBtnBG are the images to toggle.
This seems to work fairly well:
UIBarButtonItem *oldButton = [myToolBar.items objectAtIndex:1];
[myToolBar setItems:[NSArray arrayWithObjects:[myToolBar objectAtIndex:0], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:#selector(tapStopRefreshButton:)],nil] animated:NO];
[oldButton release];
In this example I had a toolbar for a UIWebView and when someone clicked Reload I wanted it to change to Stop. The toolbar only had a flexible space and the one button on it - to right-align the button - so I grabbed a reference to the old button, made a new one with the same selector as the old, reset the buttons on the tab bar, and then released the original button.
Not the prettiest, but you can use all standard buttons without having to override the button class(es).
What about 2 stacked toolbars? Then you can have some system buttons in the top one, and others in the bottom one. If the play button is pressed, then just hide the top toolbar.

UIBarButtonItem does not appear to respond to "style" property

Situation: I'm placing an instance of a system "item action" button into the right navigation button slot... no problems there. However, I want that button to display as just the icon WITHOUT a border around it (ie: "plain" style). Reading over documentation, it sounds like this should be a simple matter of just setting the UIBarButtonItem's "style" property to UIBarButtonItemStylePlain, like so:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(didPressShare)];
shareButton.style = UIBarButtonItemStylePlain;
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
However, when I implement the above code, the button appears in the nav bar with a border around it... apparently the system is not observing my UIBarButtonItemStylePlain setting. Any ideas as to why? Any other solutions for making a button appear a just the icon and no border around it?
Thanks in advance!
I'm sorry to tell you, but as far as I know you can't use plain style with UINavigationBar.
If it is possible use UIToolbar instead.

How do I set the color for the editButtonItem in the navigation bar of a UITableViewController?

I have a UITableViewController where I added a "editButtonItem" in the navigation bar :
self.navigationItem.leftBarButtonItem = self.editButtonItem;
No magic here, but I try to define the color (background and foreground/textcolor) of this button.
I read in the Apple forum somewhere that the button changes the color if I change the navigationbar to the same color, well despite the fact that I do not get this to work either (for testing) I do not want to touch the navigationbr itself, just the button.
Since this button is already predefined I am not sure how to handle this. Do I need to overwrite the button with my own definition or can I just simply apply a new style (if so how ?)
Thx
If everything fails, you can tint the bar in the viewDidLoad of your childViewController
[self.navigationController.navigationBar setTintColor:[UIColor colorWithHue:50 saturation:50 brightness:.2 alpha:.5]];
The only way I can find to make a UIBarButtonItem with a different color is to use a custom view and the initWithCustomView: method. Predefined buttons and those created using initWithTitle:target:action: cannot change their background color.
Take a look at this SO question: UIBarButtonItem with color?
These links might also help:
http://www.iphonedevsdk.com/...
http://www.insanelymac.com/...

Can't set color for UIBarButtonSystemItem

I have been trying to have a UIBarButtonSystemItem in my toolbar and it always defaults to the standard blue. If I create it with an image or title it works fine with the correct color, try it with UIBarButtonSystemItem and it goes to the default color and nothing I have found on the internet works.
heres the code:
UIBarButtonItem *overlays = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:#selector(overlays)];
Had to create my own button. Seems to be a bit of a bug that initWithBarButtonSystemItem default back to the standard colour. Few people have requested to Apple to fix it but no luck so far.
I realize this is old, but the color of the button is dependent on the color of the navigationbar. You must create a custom button like you did. This is most likely a Apple HIG deal.