UIBarButtonSystemItemCamera is not centered on UIToolBar - iphone

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

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];

display keyboard with button list on the top

I hope to push keyboard with button list on the top of the keyboard.
Please refer to the image
Is it possible?
you need to add a toolbar. Here is an example I used for an action sheet - you can make the modifications for the keyboard easily:
//DEFINING TOOL BAR
UIToolbar * keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
keyboardToolbar.barStyle = UIBarStyleBlackOpaque;
[keyboardToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissKeyboard:)];
[barItems addObject:doneBtn];
[keyboardToolbar setItems:barItems animated:YES];
[keyboard addSubview:keyboardToolbar];
You can obviously add more items to the UIToolBar for "Next", "Previous", etc.
It is possible. Here is a link to a keyboard controls project on github.

how to add 4 buttons in UInav bar for iphone

i want to add 4 buttons in UINAV bar how to do that
should i make them in UINAVBAR controller or navItem or UIBarbutton???
any code will be appreciated
Thanks
UINavigationBar is used to navigate backward or forward in application. You can't use it for any other functionality. How ever if you want to show four button on top use UIToolBar. A tool bar can multiple button with different functionality.
` create toolbar using new
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressButton2:)];
UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:#selector(pressButton3:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];
//release buttons
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[flexItem release];
//add array of buttons to toolbar
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];`

UINavigationController Toolbar : problem setting UIBarButtonItems

-(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