blank Toolbar from setItems - ios

My UINavigationController toolbar is blank and no items show up on it. Here is my code
self.navigationController.toolbarHidden = NO;
UIToolbar* toolbar = self.navigationController.toolbar;
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
[toolbar setItems:items animated:YES];

That's not the proper way to setup the toolbar. UIViewController has a toolbarItems property. The nav controller will use that property to automatically populate its toolbar.
Your code should be:
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *btnTest = [[UIBarButtonItem alloc] initWithTitle:#"Test"
style:UIBarButtonItemStylePlain
target:self
action:#selector(buttonPushed)]];
self.toolbarItems = #[ btnTest ];

Related

Flexible bar button item now displayed on UIToolbar

I know it may seem very simple and has been asked many times but i can't get it to work .I want to add two buttons to my toolbar. One on the right side and the other one on the left. Here is the code but the flexible one which is supposed to show up on the right side doesn't appear at all. Here is the code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:#selector(showSearch:)];
[done setImage:[UIImage imageNamed:#"cancel"]];
[items addObject:done];
[toolbar setItems:items animated:NO];
You need to use UIBarButtonSystemItemFlexibleSpace instead of UIBarButtonSystemItemFixedSpace
Code:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];
Reference : UIBarButtonItem Class Reference
The item with the system item UIBarButtonSystemItemFixedSpace is a space, not button object. Therefore it does show up. It's just no button.
Change your code to
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"next" style:UIBarButtonItemStylePlain target:self action:#selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"cancel" style:UIBarButtonItemStylePlain target:self action:#selector(showSearch:)]];
[toolbar setItems:items animated:NO];

UIActivityViewController make UIBarItem in inputAccessoryView disappear?

I use UIToolbar as inputAccessoryView of UITextView, there are some UIBarItems inside it.
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *bold = [[UIBarButtonItem alloc] initWithTitle:#"B" style:UIBarButtonItemStyleBordered target:self action:#selector(bold)];
UIBarButtonItem *italic = [[UIBarButtonItem alloc] initWithTitle:#"I" style:UIBarButtonItemStyleBordered target:self action:#selector(italic)];
UIBarButtonItem *underline = [[UIBarButtonItem alloc] initWithTitle:#"U" style:UIBarButtonItemStyleBordered target:self action:#selector(underline)];
[items addObject:underline];
[items addObject:italic];
[items addObject:bold];
self.toolbar.items = items;
[self.textView setInputAccessoryView:self.toolbar]
These items work OK, but when UIActivityViewController active, then dismiss in iOS6 simulator, these items are disappear, only blank toolbar display.
What's the reason? Please help!

How to create Navigation using ToolBar button

I have created a Toolbar button and given action() to navigate to other ViewController.But the code is not working for Navigation.And not able to assign a image to the button.How can i solve this?
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 320, 44);
[self.view addSubview:toolbar];
[toolbar release];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress:)];
NSMutableArray *toolbarItems = [NSMutableArray arrayWithObjects:customItem, nil];
[toolbar setItems:toolbarItems animated:NO];
}
-(void)updateAddress{
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]init];
UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:updateView];
[self.navigationController popToViewController:navi animated:YES];
}
First you should set the selector right, you should take out the : from the selector because you don't have parameters for the updateAddress method
should be like this
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonItemStyleBordered target:self action:#selector(updateAddress)];
Then You need to initialize the DisplayMapViewController with its NibName
DisplayMapViewController *updateView=[[DisplayMapViewController alloc]initWithNibName:#"DisplayMapViewController" bundle:nil];
and then just pop to the view you are intended to.
[self.navigationController popToViewController:updateView animated:YES];
if you have already rootViewController then you could just popToRootViewController

UIToolbar creation

i have created a toolbar as below shown code and now i dont knw how to add the items to it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 350, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] init] autorelease]];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
Try this code
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[toolbar setItems:items animated:NO];
[customItem release];
You can also refer to this link:
How to create this toolbar programmatically
Please do google search and Stack Overflow search before posting.
use
[self setToolbarItems: buttons animated:NO];
as u want to set toolbaritems and uiviewcontroller has toolbar itself.

ios6 UINavigationBar with more than 2 buttons

in my app i have a uinavigationbar with 3 buttons, but the code i used for ios5 does not seem to be good for ios6 anymore.
You can see a wrong-colored rect underneath my custum-view with two buttons on the right side.
heres the code i used, i found it somewhere on stackoverflow:
UIToolbar *tools = [[[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 110.0f, 44.01f)] autorelease]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = NO;
tools.translucent = self.headerbar.translucent;
tools.barStyle = self.headerbar.barStyle;
tools.backgroundColor = self.headerbar.backgroundColor;
tools.tintColor = self.headerbar.tintColor;
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create a standard refresh button.
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithTitle:#"heute" style:UIBarButtonItemStylePlain target:self action:#selector(showThisWeek)];
bi.style = UIBarButtonItemStyleBordered;
bi.width = 0;
[buttons addObject:bi];
//[bi release];
// Create a spacer.
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:#selector(showThisWeek)];
[buttons addObject:bi];
//[bi release];
// Add profile button.
bi = [[UIBarButtonItem alloc] initWithTitle:#"vor" style:UIBarButtonItemStylePlain target:self action:#selector(showNext)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
//[bi release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
//[tools release];
header.rightBarButtonItem = twoButtons;
[header.leftBarButtonItem setAction:#selector(showPrevious)];
can someone hint me to a more elegant solution that works fine on both ios5 and ios6?
cheers
lukas
Did you try leftBarButtonItems and rightBarButtonItems? You can add array of buttons in left and right side nav bar using this.
If you use this, there will be no need for UIToolbar and their subviews.
Have you tried bar.style = -1?

Resources