I have used a UIBarButtonItem in navigation controller. It look like as
and my code was
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneImagePicker)];
[self.navigationItem setRightBarButtonItem:doneButton];
[doneButton release];
I dont need white coloured box over Done button. How to fix it.
update:
For some reason it's style is being set incorrectly.
You should set it to UIBarButtonItemStylePlain like this:
donButton.style = UIBarButtonItemStylePlain;
Related
I had add image at my navigation bar and need to add the right bar refresh button.
But the right bar button can not show at the navigation after add.
Why?
Here the code add image at navigation bar:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"topbar.png"] forBarMetrics:UIBarMetricsDefault];
Here the add right bar button at viewDidLoad:
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
NSArray *actionButtonItems = #[refreshItem];
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
You should not add to self.navigationcontroller.
Do something like this will resolve your problem.
self.navigationItem.rightBarButtonItem = refreshItem;
Try with following code
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh: )];
self.navigationItem.rightBarButtonItem = refreshItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightButtoneClicked:)];
self.navigationItem.rightBarButtonItem = rightButton;
Right this code in viewDidLoad and you get the refresh button on right side.
Replace this
self.navigationController.navigationItem.rightBarButtonItems = actionButtonItems;
With:
self.navigationItem.rightBarButtonItems = actionButtonItems;
In UITableView I configure self.navigationItem.leftBarButtonItem with a button. Now I want to reload this button to update text on this button, but it's on Navigation bar. I can update UITableView by
[self.tableView reloadData]
but How to update Navigation bar?
Update: I'm NOT init the title of button, but reload it. So any init code (such as self.navigationItem.leftBarButtonItem.title = #"balabala") doesn't work, because it does NOT take effect immediately.
Just change the text on the leftBarButtonItem?
self.navigationItem.leftBarButtonItem.title = #"Back"; // or whatever text you want
Try this
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(OnClick:)];
self.navigationItem.leftBarButtonItem = btnBack;
Hope this helps you..
EDIT :-
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle: #"Title" style: UIBarButtonItemStyleBordered target:self action:#selector(OnClick:)];
[[self navigationItem] setBackBarButtonItem: btnBack];
Did you try setting the text, using
self.navigationItem.leftBarButtonItem.title
You need to use the following statement:
self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];
In other words you need the setLeftBarButtonItem:animated message instead of the standard one. This will reload the button text with animation. Same applies for the back button if that is what you are interested in changing at some point.
#interface Myviewcontroller : UIViewController
{
UIBarButtonItem *barBtnBack;
UIButton *btnQuestion;
}
//ViewdidLoad
btnQuestion = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[btnQuestion setBackgroundImage:[UIImage imageNamed:#"question-mark2x"] forState:UIControlStateNormal];
[btnQuestion addTarget:self action:#selector(goBacktomain)
forControlEvents:UIControlEventTouchUpInside];
barBtnBack =[[UIBarButtonItem alloc] initWithCustomView:btnQuestion];
[self.navigationItem setLeftBarButtonItem:barBtnBack];
note- change btnQuestion title when you want to change navigationnar button title
If you want to change the NavigationBar title, try 1 of these:
[self.navigationController.navigationItem setTitle:#"Title"];
[self.navigationItem setTitle:#"Title"];
[self setTitle:#"Title"];
If you wanna change button titles, put this in the same class:
- (void)setLeftBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setLeftBarButtonItem:leftButton];
}
- (void)setRightBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setRightBarButtonItem:rightButton];
}
Examples to set button title and actions:
// Change button titles and actions any time
[self setLeftBarButtonTitle:#"Back" action:#selector(goBack)];
[self setRightBarButtonTitle:#"Something" action:#selector(doSomething:)];
// Change button titles with no actions
[self setLeftBarButtonTitle:#"Nothing" action:nil];
[self setRightBarButtonTitle:#"Nothing" action:nil];
// Remove buttons from navigation bar
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:nil];
Try this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"your_title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
I solved it in Swift, but if the keyboard is shown, it hides.
navigationController?.loadView()
I need to use a simple UIBarButtonItem in a UIToolBar.
I used this code to add the button with my custom image to the navigation bar:
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image_sheep.png"] style:UIBarButtonItemStylePlain target:self action:#selector(clone)];
NSArray *rightItems = [NSArray arrayWithObject:cloneButton];
self.navigationItem.rightBarButtonItems = rightItems;
The result is what I want and it looks like this
navigation bar http://img207.imageshack.us/img207/3383/navigationbara.jpg
When doing the same thing in a UIToolBar that I'm adding to a UITableViewCell's contentView
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image_sheep.png"] style:UIBarButtonItemStylePlain target:self action:#selector(clone)];
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = [NSArray arrayWithObjects:leftSpace, cloneButton, nil];
The problem is that I get something like this:
toolbar http://img209.imageshack.us/img209/1374/toolbary.jpg
This is surely due to the fact that UINavigationBar and UIToolBar are not drawn the same way...
Can someone please point out how to resolve this problem?
On your UIToolbar use UIBarButtonItemStyleBordered instead of UIBarButtonItemStylePlain.
UINavigationBar will not draw the button the same without the button look.
That's how UIToolbar is supposed to work. Per the documentation:
Toolbar images that represent normal and highlighted states of an item derive from the image you set using the inherited image property from the UIBarItem class. For example, the image is converted to white and then bevelled by adding a shadow for the normal state.
That said, you might be able to get the result you want by creating a UIBarButtonItem with a custom view instead of an image. Like so:
UIImage *sheepImage = [UIImage imageNamed:#"image_sheep.png"];
UIButton *sheepButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sheepButton setImage:sheepImage forState:UIControlStateNormal];
[sheepButton addTarget:self action:#selector(clone) forControlEvents:UIControlEventTouchUpInside];
[sheepButton setShowsTouchWhenHighlighted:YES];
[sheepButton sizeToFit];
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithCustomView:sheepButton];
I haven't tested this, so I don't know if it will work.
I cannot see the images posted in the question anymore. But I am pretty sure that we're trying to mimic the UIBarButtonItemStylePlain of the UIToolBar in a UINavigationBar.
Benzado is pretty much spot on except for this line:
[sheepButton setImage:sheepImage forState:UIControlStateNormal];
This puts the image in front of the touch indicator. The UIToolBar shows the touch in front of the image. So to mimic the UIToolBar style in a UINavigationBar:
UIImage *sheepImage = [UIImage imageNamed:#"image_sheep.png"];
UIButton *sheepButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sheepButton setBackgroundImage:sheepImage forState:UIControlStateNormal];
[sheepButton addTarget:self action:#selector(clone) forControlEvents:UIControlEventTouchUpInside];
[sheepButton setShowsTouchWhenHighlighted:YES];
[sheepButton sizeToFit];
UIBarButtonItem *cloneButton = [[UIBarButtonItem alloc] initWithCustomView:sheepButton];
So I have two buttons in the leftBarButtonItem. This is my code.
NSString *todayString = #"...";
UIBarButtonItem *todayButton = [[[UIBarButtonItem alloc] initWithTitle:todayString style:UIBarButtonItemStyleBordered target:self.calendarController action:#selector(todaySelected)] autorelease];
NSString *weeklyString = #"...";
UIBarButtonItem *weeklyButton = [[[UIBarButtonItem alloc] initWithTitle:weeklyString style:UIBarButtonItemStyleBordered target:self action:#selector(weekSelected)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] init] autorelease];
[toolbar setItems:[NSArray arrayWithObjects:todayButton, weeklyButton, nil]];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease];
The title of the buttons is dynamic. Sometimes the titles can get somewhat long. The buttons expand and look fine. However, if the titles get too long, it seems that the UIToolbar reaches some width maximum, as the buttons stop responding to taps at a certain X point. See image here
Green represents responds to touch and red does not respond to touch. The Today button responds to touch. The Weekly button, however, only responds to touch until the second "e". Anything after that does not respond to touch.
I've been banging my head trying to fix this for a while, with no luck. I tried expanding the frame of both the UIToolbar and the leftBarButtonItem. Anyone know whats going on? Thanks in advance
Your toolbar frame width is probably too narrow. In your code, you don't even set its size, and rely on it to be sized it for you. Trying setting it to larger width, and make sure the autoresizingMask does not have `UIViewAutoresizingFlexibleWidth' set.
A good debugging technique is to set the background color of your new toolbar to something bright (to contrast against the toolbar you are installing it into) and watch its sizing to see how your button is being clipped interaction-wise.
You may need to explicitly resize it as you update the bar button item titles if fixing it to a certain width is too restrictive for your app.
You'll need to make a custom button view that is actionable, with something like a UIButton:
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[newButton addTarget:self action:#selector(newAction:) forControlEvents:UIControlEventTouchUpInside];
[newButton setTitle:#"New Button" forState:UIControlStateNormal];
UIBarButtonItem *newBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:newButton];
self.navigationItem.leftBarButtonItem = newBarButtonItem;
for example.
EDIT - now I see what you are doing, I apologize for the earlier confusion. You are adding an array of buttons to the left side. Try adjusting the frame of the toolbar with so the buttons fit inside the interactive area:
NSString *todayString = #"...";
UIBarButtonItem *todayButton = [[UIBarButtonItem alloc] initWithTitle:todayString style:UIBarButtonItemStyleBordered target:self action:nil];
NSString *weeklyString = #"...";
UIBarButtonItem *weeklyButton = [[UIBarButtonItem alloc] initWithTitle:weeklyString style:UIBarButtonItemStyleBordered target:self action:nil];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar setFrame:CGRectMake(0, 0, 320, 50)];
[toolbar setItems:[NSArray arrayWithObjects:todayButton, weeklyButton, nil]];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
I know this is an old question, but it led me astray for a bit until I found updated information. I could not use the technique mentioned above in iOS 7 because the embedded toolbar would not line up with its parent.
Anyhow, iOS 5 (I believe) added button arrays for the left and right items:
UIBarButtonItem * trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:#selector(onTrashClick:)];
UIBarButtonItem * mailItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(onActionClick:)];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
When an item from a UITableView is selected, a detail view is loaded which has a banner bar at the top with a back button on it to navigate back to the table.
How do I add other buttons to that banner bar?
As Andrew said, you can add custom views to the navigation bar. For example, if you are looking to add multiple buttons to the right side of the navigation bar, you can do something like this:
// right side of nav bar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 106, 44)];
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAction:)];
cancelButton.style = UIBarButtonItemStylePlain;
[buttons addObject:cancelButton];
[cancelButton release];
[toolbar setItems:buttons animated:NO];
toolbar.barStyle = -1;
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Make sure to adjust the width (106 above) if you need more or wider buttons, and then provide methods for the selectors (deleteAction: and cancelAction: above).
From your description, it sounds like you have a UINavigationController correctly set up. That "banner bar" you are describing is properly called a navigation bar.
From within the detail view, you can use:
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:"HiMom" style:UIBarButtonItemStylePlain target:self action:#selector(onHiMom:)] autorelease];
self.navigationItem.rightBarButtonItem = button;
You can also add custom views (instead of a button) plus a few other settings. I suggest poking around the documentation for UINavigationItem, UINavigationBar, and UIBarButtonItem for ideas.