UINavigationController Toolbar : problem setting UIBarButtonItems - iphone

-(void)viewWillAppear:(BOOL)animated {
//setup toolbar
[self.navigationController setToolbarHidden:NO];
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.toolbar.alpha = .8;
self.navigationController.toolbar.tintColor = [UIColor colorWithRed:6.0/255.0 green:95.0/255.0 blue:163.0/255.0 alpha:1];
//setup items
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self.navigationController.toolbar action:nil];
UIImage *imageUp = [UIImage imageNamed:#"up.png"];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:imageUp style:UIBarButtonItemStylePlain target:self action:#selector(toggleUp:)];
item2.width = 5;
UIImage *imageDown = [UIImage imageNamed:#"down.png"];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithImage:imageDown style:UIBarButtonItemStylePlain target:self action:#selector(toggleDown:)];
UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
UIBarButtonItem *item6 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(toggleDown:)];
UIBarButtonItem *item7 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *itemsArray = [[NSArray alloc] initWithObjects:item1, item2, item3, item4, item5, item6, item7, nil ];
[self setToolbarItems:itemsArray];
...more
}
I am not sure what is happening here and I have read a lot about what to do for the toolbar but I cannot seem to get any of the UIBarButtonItems to display.
Any ideas?

Sorry for the messed up code above.
I have found the answer to my question.
I was accessing a child of the UINavigationController and not the root UIView.
I assigned the code above to the viewWillAppear to the root UIViewController of my UINavigationController and it worked. So to show it only on my sub UIViewController I had to set
[self.navigationController setToolbarHidden:NO];
To all the UIViewControllers that I do not want the toolBar to be visible on.
Hope this helps someone out. Thanks

Related

How to add bar buttons in a UIToolBar

I have created a UIToolBar and want to add three items in that like contact, date and message. I tried but i am not able to do that.Kindly help as i am new to Objective C. Here is my "ViewController.m"
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 414, self.view.frame.size.width, 44);
UIBarButtonItem *contact = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:contact,message, nil];
[toolbar setItems:items animated:NO];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
In Following code i added two UIBarButton with flexSpace..
you can add UIBarButton as you want
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
Toolbar.barStyle = UIBarStyleBlackTranslucent;
[Toolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(Cancel)];
[barItems addObject:btnCancel];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(done)];
[barItems addObject:btnDone];
[Toolbar setItems:barItems animated:YES];
Following Method is call when tapped on bar Button
-(void)Cancel
{
// Write Code for Cancel Method
}
-(void)done
{
// Write Code for Done Method
}
try this...
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
myToolbar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *contactBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(contact:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *dateBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(date:)];
UIBarButtonItem *flexibleSpaceRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *msgBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(message:)];
[myToolbar setItems:[NSArray arrayWithObjects: flexibleSpaceLeft, doneBtn, flexibleSpace, dateBtn, flexibleSpaceRight, msgBtn, nil]];

Is it possible to add title to toolbar button, declared as UIBarButtonSystemItem

I'd like to set some buttons in my navigation toolbar and, for users not be confused of what each button do, I'd like to add title to each button, displayed under it. Is it possible to do this?
Declaration of button:
UIBarButtonItem *changeFiltersButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButton)] autorelease];
``...
[self setToolbarItems:[NSArray arrayWithObjects: flexible,
changeFiltersButton, flexible, flexible,
refreshFiltersList, flexible, flexible,
more, flexible, nil]];
use images with name like the fallowing example
NSMutableArray *toolBaritems = [[NSMutableArray alloc] init];
[toolBaritems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]];
[toolBaritems addObject:[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"img.png"] style:UIBarButtonItemStylePlain target:self action:#selector(refresh)]];
[toolBaritems addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]];
[toolBaritems addObject:[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"img2.png"] style:UIBarButtonItemStylePlain target:self action:#selector(change)]];
[toolbar setItems:toolBaritems animated:NO];

Align buttons or space between buttons in NavigationBar programmatically

I have created navigation controller via storyboard interface and I have added 4 buttons programmatically on Navigationbar but I don't know how should I justify it would you please help me!
here is the picture:
here is my code for buttons I know That I used rightBarButtonItems but I don't know what should I write instead!
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *menuButton= [[UIBarButtonItem alloc] initWithTitle:#"Menu" style:UIBarButtonItemStyleDone
target:self action:#selector(menu:)];
UIBarButtonItem *yearButton= [[UIBarButtonItem alloc] initWithTitle:#"Year" style:UIBarButtonItemStyleDone
target:self action:#selector(year:)];
UIBarButtonItem *weekButton= [[UIBarButtonItem alloc] initWithTitle:#"Week" style:UIBarButtonItemStyleDone
target:self action:#selector(week:)];
UIBarButtonItem *reportButton= [[UIBarButtonItem alloc] initWithTitle:#"Report" style:UIBarButtonItemStyleDone
target:self action:#selector(report:)];
NSArray *buttons = [NSArray arrayWithObjects:menuButton,yearButton,weekButton,reportButton,nil];
self.navigationItem.rightBarButtonItems = buttons;
}
Add flexible spacers between each of the buttons:
UIBarButtonItem *flexibleSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonSystemItemCamera is not centered on UIToolBar

I've got very simple code here
[mImagePickerToolBar setBarStyle:UIBarStyleBlackTranslucent];
[mImagePickerToolBar sizeToFit];
UIBarButtonItem *spaceItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *spaceItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(captureBarItemPressed:)];
NSArray *items = [NSArray arrayWithObjects: spaceItem1, cameraItem, spaceItem2, nil];
[mImagePickerToolBar setItems:items animated:NO];
on my UIToolBar camera item is not centered at all, see on the picture, in comparison with home button its a little bit right, why is this so and how to center it ?
 
The flexible spaces should work. This is a bug in iOS, in my opinion. You can see for yourself by mocking this up in interface builder.
Here is the bordered button:
And here is the plain button:
It isn't even lined up within its own selection area.
[self.navigationController setToolbarHidden:NO];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 436, 320, 44);
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
spaceItem.width = 130.0;
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(captureBarItemPressed:)];
NSArray *items = [NSArray arrayWithObjects: spaceItem, cameraItem, nil];
[toolbar setItems:items];
[self.navigationController.view addSubview:toolbar];
This is working for me. Please make sure at your side

Buttons don't appear on the toolbar

A simple question. How do I add toolbar items in combination with a TTLauncherViewController. I must do something very basic wrong, as the toolbar appears, but the button does not show:
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.tintColor = [UIColor blackColor];
UIBarButtonItem *updateBttn = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(updateData:)];
UIBarButtonItem *btnSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil ];
self.navigationController.toolbarItems = [NSArray arrayWithObjects:updateBttn, btnSpacer, nil];
[btnSpacer release];
[updateBttn release];
Could this be the issue?
http://www.iphonedevsdk.com/forum/iphone-sdk-development/22180-navigationcontroller-toolbar-wont-show-any-buttons.html
In short, it says that the toolbar is not a child of the navigation controller but the parent view.