Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom background/transparent background on UIBarButtonItem?

How is this effect achieved? Code snippets are highly welcome.

This can be seen in Notes app and other apps on the App Store.

It seem that either the buttons on the navigation controller are transparent and show the custom background of the navigation controller or that they themselves have a custom background applied to them. Any ideas on how this is achieved?

EDIT:

Here is an example from the iBooks app for the iPhone. As you can see, not only the UIButtons, but also the segmented control have custom backgrounds. I'm thinking that there is a way to globally set transparency/background to different interface elements.

examplehttp://c0839932.cdn.cloudfiles.rackspacecloud.com/iBooks%201.1%20-%206.png

like image 277
Gene K Avatar asked Jun 20 '10 22:06

Gene K


2 Answers

If you want to have UIBarButtonItem everywhere in your application, what you can do is in your AppDelegate.m and in your application:didFinishLaunchingWithOptions: method, you can write

[[UIBarButtonItem appearance] setBackgroundImage:[[UIImage alloc] init] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

This will create an empty image, and it will set UIBarButtonItem's background to that image. With writing it into AppDelegate you will make sure everywhere you are using a UIBarButtonitem it will have transparent background, even your back buttons.

like image 119
Umut Sirin Avatar answered Sep 20 '22 09:09

Umut Sirin


I don't think you can make it transparent just by setting some sort of property (but I might be wrong).

Look at the UIBarButtonItem documentation. There are init methods that will take a custom view or an image and create a UIBarButtonItem from it. So create a view or an image with the appearance you want and use that. One thing you might want to try is to create a UIButton the way you want it to look, and then do [[UIBarButtonItem alloc] initWithCustomView:button];

Another solution is to sent the tintColor property of your UINavigationBar to whatever color you want, and the button will take on that color.

like image 28
indragie Avatar answered Sep 19 '22 09:09

indragie