Disable Border Around System Image Located UINavigationBarItem - iphone

I want to disable the border around iPhone SDK's add button, so it's just the "+" button (no black bg):
This seems to be a simple task when it's located in the toolbar, but not when it's located in the UINavigationBar. Anyways, if anyone knows how, or if it's even possible, then please share! Here's my current code in my RootViewController.m:
self.title = #"Code Master";
addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addButtonClicked:)];
self.navigationItem.rightBarButtonItem = addButton;
EDIT
#Dee so your code works great, displays exactly what I want! There is another problem now that I'll explain:
Edit Button: Left side of UINavigationBar
Done Button: Left side of UINavigationBar (hidden); when the Edit button is clicked, it replaces the Edit Button with this "Done" button
Plus Button: Right side of UINavigationBar; hides when in "edit mode", shows when in "normal mode (when Done button is not visible)"
So basically my problem now is that whenever I click Edit, then Done, the plus button doesn't return from hiding. I'm using your exact code and it worked with my previous code. I'm pretty sure that it's because in your code, theres nothing labelling it "addButton". Here's the code later on in my main file:
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:YES];
if (editing)
{
self.navigationItem.rightBarButtonItem = nil;
}
else
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Plus.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 29)];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = barBtn;
[barBtn release];
}
[self.tableView reloadData];
}

Create "+" image and set it to button & give that button to UIBarButtonItem like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Plus.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 29)];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barBtn];
[barBtn release];

Related

Adding a button with backgroundimage and action in navigation bar

I need to add a button with no title and background image and perform action when clicked on it .I need to add a button to navigation bar.
I have made the navigation bar like this :
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationBar setFrame:CGRectMake(0,0,320,50)];
self.navigationBar.tintColor=[UIColor whiteColor];
}
I can see the white navigation bar to it.How can I add a button to it with image and action ?
use this code
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationBar setFrame:CGRectMake(0,0,320,50)];
self.navigationBar.tintColor=[UIColor whiteColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"yourImageName"]
style:UIBarButtonItemStyleBordered
target:self action:#selector(yourButtonPressMethodName:)];
self.navigationItem.leftBarButtonItem = item;//use rightBarButtonItem if you want to add button on right dife
}
You can make UIBarButtonItem with custom view:-
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0, 0.0, 75.0, 40.0)];
[btn setImage:[UIImage imageNamed:#"anyImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = barbutton;
you can use code below:
UIBarButtonItem *rightBar=[[UIBarButtonItem alloc]initWithImage:#"img.png" style:UIBarButtonItemStyleBordered target:self action:#selector(methodname:)];

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

How to hide default navigation bar button and add custom button in it?

I have one navigation based application in which i want to hide the default navigation
which is display at left side and i want to add my own custom button.I have written following 2 code.
code sample 1:
- (void)viewDidLoad {
appDelegate=[(FoodAppDelegate *)[UIApplication sharedApplication]delegate];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Volunteers_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 86, 30)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
But this displays default navigation button.
second code sample:
self.navigationItem.hidesBackButton=YES;
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"DontWorryAboutThis" style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
[barButton setImage:[UIImage imageNamed:#"Volunteers_back.png"]];
[self.navigationItem setLeftBarButtonItem:barButton];
This one displays both button default and custom overlapping each other.
Does anyone have any idea what is the problem here?or any sample code to achieve this?
You shouldn't have to hide the back button. If you replace the leftBarButtonItem, it should overwrite whatever button is there. I use this code, and ONLY this code to accomplish this in several of my apps.
UIBarButtonItem *btnCancel =
[[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style: UIBarButtonItemStylePlain
target: self
action: #selector(actionButtonCancel)];
self.navigationItem.leftBarButtonItem = btnCancel;
[btnCancel release];

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.

custom back button image does not work on the navigation bar

I created a custom navigation back button. But when I add this code, my back animation doesn't work and it goes back with animation. Why? I think it depends on my #selector action.
- (void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
ok i created a function and works fine :D :
-(void)pushNav {
[self.navigationController popViewControllerAnimated:YES];
}