Image on UIBarButtonItem - iphone

In my iPhone application, I'm adding a toolbar on UINavigation controller as navigation bar do not take more then two bar buttons on it.
So far .. I have successfully added the UIToolBar with three BarButtonItems on it. However I am not able to display an image on BarButtonItems.
Can any one tell me .. how to add image on UIToolBar BarButtonItems? Where ToolBar is already added on nav controller.

Set a custom view for the UIBarButtonItem, something like this
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *yourImage = [UIImage imageNamed:#"yourImage.png"];
[button setImage:yourImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(yourButtonAction) forControlEvents:UIControlEventTouchUpInside];
[barButton setCustomView:button];
self.navigationItem.rightBarButtonItem = barButton;
[barButton release];

button = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:image]];
button.action = #selector(myaction);
button.target = self;
Try this.

Related

Both a UIImage and a "back" button appearing in my viewController

I have both a UIImage and a "back" button appearing in my viewcontroller and can't figure out why.
here's a screenshot:
here's my code:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
UIImage *image = [UIImage imageNamed:#"arrow_back.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
}
thanks for any help.
You will have to create a backbarbutton item and add a custom to it. Then you define the action for it, where you pop the current view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// selector for backButton
-(void)back{
[self.navigationController popViewController];
}
You need to create a custom button and add it as a subview of self.navigationController.navigationBar.backBarButtonItem.
The back button is automatic when you push a second view controller into the navigation controller. You can override it as Andrew Ebling mentioned. You will need to create a custom UIBarButton item.
UIImage *image = [UIImage imageNamed:#"back.png"];
UIBarButtonItem *back = [UIBarButtonItem alloc] initWithImage:image
style:UIBarButtonItemStylePlain
target:self
action:#selector(backEvent:)];
self.navigationItem.backBarButtonItem = back;

info light button in navigation bar

How can I change this code so I have an info button in the right corner of the navigation bar instead of a word?
- (void)viewDidLoad {
[self setTitle:#"About"];
[super viewDidLoad];
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"info", #"")
style:UIBarButtonItemStyleBordered
target:self
action:#selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
}
This question has often been asked.
UIImage *buttonImage = [UIImage imageNamed:#"UIButtonInformationIcon.jpg"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(anyActionYouWant) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button]autorelease];
Here's one solution, among several.
Or... you could select from the drop-down list when adding a button to the title bar, which kind of button you want. You'd need to select the type "Info Light".

Can't see my navigationbar custom button, but it is clickable

I want to add a custom button (to the right) in my navigation bar.
I have the following code in my "viewDidLoad" function :
UIButton *audioBtn = [[UIButton alloc] init];
[audioBtn setTitle:#"Play" forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"play.png"];
[audioBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
I can't see the button in my navigation bar, but if I click to the right (where the button is supposed to be), it triggers the function "toggleAudioPlayback", so the only problem is that I don't see the button!
I tried with different image, setting a background color, nothing works...
By the way, I use this image somewhere else in the code, and I see it (on a custom button, but not in a navigationBar).
Help me please!
Make your life easier and instead of a customView just use a UIBarButtonItem with a custom image:
UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"play.png"] style:UIBarButtonItemStylePlain target:self action:#selector(toggleAudioPlayback:)];
self.navigationItem.rightBarButtonItem = audioBtn;
[audioBtn release];
UPDATE
Since you have indicated you want your button to not have a border, then you will need to use a CustomView afterall, but I have modified your code to make this work (the key difference is the assigning of the frame, which is set to the size of the image, but you could set it to a custom size to have the image centered):
UIButton *audioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *playImg = [UIImage imageNamed:#"play.png"];
[audioButton setBackgroundImage:playImg forState:UIControlStateNormal];
[audioButton setBackgroundImage:playImg forState:UIControlStateHighlighted];
audioBtn.frame = CGRectMake(0,0,playImg.size.width,playImg.size.height);
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
Instead of [[UIButton alloc] init] try the +buttonWithType: method on UIButton.

Change color of UIBarButtonSystemItemCancel

If I use the UIBarButtonSystemItemCancel, it's color is set to that of the navigation bar. However, in the Photos App, the "Cancel" button is shown with a blue background. How can I also set the UIBarButtonSystemItemCancel to have a blue background please?
your can use image as button image as
UIImage *buttonImage = [UIImage imageNamed:#"image.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(clickActionItem:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button ];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
set the theNavigationController.navigationBar.barStyle to UIBarStyleBlackTranslucent
theNavigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
and the buttons will be blue by default.
There is a better way than using an image. The UISegmentedControl can be configured to look just like a UIBarButtonItem and UISegmentedControl has a tintColor property.
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *removeButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
I built a UIBarButtonItem category that'll do this.

UIButton as UINavigationbar button

I have a Tab bar application. and in one of the tab bars I have a navigation contorller.
Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:#"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT:
I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton and use it as custom view in the UIBarButtonItem. You can create a UIBarButtonItem directly with an image:
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView as custom view in the UIBarButtonItem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:#"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted property of the UIButton to visually hightlight the button when it is pressed.
I ended up doing this
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.