I have a button as the right button on my navigation bar:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStyleBordered target:self action:#selector(addToFavouritesButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
All good, except I'd like it to be an image of a star with a little plus on it. Thought it would be easy to just create a png and set the UIBarButtonItem's image property. Which it is, however 2 things. It puts the gloss and bevel at the left and right side of my image, but not on the image itself and secondly I can't find a recommended size of button in the docs.
Is there an easy way of creating a button image and the system to format it so it looks like it is part of the Navigation Bar? If I attempt to create the gloss and bevel in Photoshop it looks good but doesn't look quite like the system generated buttons on the left side.
Hope that makes sense and any pointers would be appreciated,
Dave
Doh, didn't have an alpha channel in my png, created a mask and all looks good.
Related
I am able to customize the back button on my navigation bar, but in order for me to show the left arrow shape, do I need to create my customized image with the left arrow specifically?
Or there is a way to use the original back button with the arrow shape, while just apply my background image onto it?
Thanks in advance!
Set yourButton the customized image with the left arrow
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:yourButton] autorelease];
You cannot modify the original BACK button.
I am facing a problem in placing an image for an UIBarButtonItem. This is the code I am uisng:
UIBarButtonItem *comoseButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"compose_new.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(composeNewMessage)];
But I am not getting the proper output: the button size is too big. How can I adjust the frame? (I'm developing for iOS 5)
I always design my bar button items to be the right size. That seems the simplest way. I don't know if you've looked it up, but they should be "approximately 20x20" according to the Human Interface Guidelines.
Other than that, I suppose you could create a properly sized UIImageView with your image, add your own responder code or gesture recognizer, and then use initWithCustomView:.
I've had trouble getting this to work, nowhere have I seen a working example on the web. Now offering bounty on this as its making me crazy. It should be easy, but that doesn't seem to be the case.
I'd like my buttons on my UINavigationBar to be semi-transparent such that they allow the background of whatever is on the UINavigationBar to show through. This effect is seen in many applications, image examples below. You can do this by setting a custom background on the item, which i think is an unacceptable solution because it requires that you prepare images beforehand, and they won't be adaptable for variable buttons etc. They will not look like Apple UI and I don't believe there is a reason to do this either, UIKit is already drawing the background for these buttons, we just need to change it. The correct solution uses the bar items and views generated by Apple's apis.
UIBarButtonItem is not a UIView subclass. When you create one and add it to a UINavigationBar, some code somewhere in the framework draws a view for it. The framework methods seem to resist anything related to allowing transparency of the bar items, such as the tintColor property.
For example, this does NOT work:
UINavigationItem *item = [[UINavigationItem alloc] init];
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"SUCKS" style:UIBarButtonItemStyleBordered target:self action:#selector(whatever:)];
editButton.tintColor = [UIColor colorWithWhite:0.4 alpha:0.3];
item.leftBarButtonItem = editButton;
Nothing I do will make UINavigationBar allow semi-transparency for its bar items. I believe at runtime we need to:
Get the image for the bar item
Mask it for transparency
Set the new image on the bar item
But I haven't been able to get the image at runtime or mask it properly. How do you do this?
Create a custom uiview and draw a semi-transparent black rectangle in it and use that view with initWithCustomView.
see
and
Failing that, you may have to use an image (png). e.g. a 1x1 black pixel png with 30% opacity.You could then initWithImage.
EDIT: I have had this second approach working using:
buttonThree = [[UIBarButtonItem alloc] initWithTitle:#" sort button " style:UIBarButtonItemStyleBordered target:self action:#selector(sortMethod)];
UIImage *thebgUIimage = [UIImage imageNamed:#"semi.png"];
[buttonThree setBackgroundImage:thebgUIimage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
This results in a button that has a transparent background image that the navbar background image shows through. However, you would need to create an image with the rounded corners on and so need an image for each button width. Also I found this thread after trying the above
A brilliant hack is to use the UISegmentedControl with a single segment (as a button) and set its tint color. Have a look at http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem. I have personally implemented this. Feel free to ask any questions.
Instead of searching for code and breaking your head, my suggestion is just to have transparent image which has just border similar to button (add shadow if necessary), create a button of custom type, add the transparent background image to it and you can text as you want. From this custom button, create your bar button item accordingly.
If you're targeting for iOS 5, you can set the background image of the button.
[_button setBackgroundImage:#"image" forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Note that you'll need to set background images for state UIControlSateSelected and again for both control states for barMetrics: UIBarMetricsLandscape, if your application allows landscape orientation.
Note again this is an iOS 5 feature.
I believe your answer is here: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
I want to put a square button in the UINavigationBar, much like the + button, but with an image I have. When i do this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settings wheel.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(prefsPressed)];
It just stretches the image out and makes a long button. Any ideas how to make it square?
UIBarButtonItem has a width property you can set. I'm not sure if that will do the trick for you, though. If not, you can go with creating a custom UIView (e.g. a UIImageView) and setting it using the initWithCustomView method:
See here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html
I was wondering why everything stops behaving as it should when I do something like this:
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
All my buttons on the navigationBar are now black regardless of style, like this one below, it should turn up as a blue button:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleDone target:self action:#selector(editTrip)];
It does not, it is just black.
The slight change/effect in the button when tapped is also gone?
Is there a way to change the color of the navigationBar and maybe even the UIbarButtonItems without somehow ruining standard behavior?
This is really strange. I just checked with the Apple "Remote" app, it has the black navigation bar and a blue "Done" button for leaving the "help" section.
Do I really have to build everything custom to achieve this?
Thanks for any help given:)
In Interface Builder, change the Style of the Navigation Bar to Black Opaque or Black Translucent and leave the tint alone. This will give you a black bar with the standard blue buttons.
I'm sure the way to do this in code without Interface Builder, is by setting the barStyle property to UIBarStyleBlack or UIBarStyleBlackTranslucent, but I haven't done it that way.