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];
Related
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];
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
I am already creating multiple buttons but I don't know how to align the buttons.
My code is here:
- (void)viewDidLoad
{
//self.title=#"Asset Management";
[super viewDidLoad];
listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:#"User Information"];
[listOfItems addObject:#"Regional Settings"];
[listOfItems addObject:#"Configuration"];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
//UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGs
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create button1
UIBarButtonItem *propertiesButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:propertiesButton];
[propertiesButton release];
// Create button2
UIBarButtonItem *commentaryButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:commentaryButton];
[commentaryButton release];
// Create button3
UIBarButtonItem *versionsButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(button3Pressed)];
[buttons addObject:versionsButton];
[versionsButton release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];
// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];
}
How do I create space b/w the buttons. Pls help me.
Thanks in advance!
You can add spaces between tool bar items using either of the two built-in space button types
UIBarButtonSystemItemFixedSpace and UIBarButtonSystemItemFlexibleSpace.
Fixed Space button
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[fixedSpace setWidth:20];
Flexible Space button
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
Add the space bar buttons in between the other tool bar items.
I want to show next previous buttons above the keyboard. I call function "makeToolBars" from viewDidLoad to create it. The application does display the buttons correctly but when I click on them I get error it does not go to "goToNextField" function.
Can anyone point how to correct it ?
-(void)goToNextField:(id)sender
{
[txtPassword resignFirstResponder];
[txtUserName becomeFirstResponder];
}
-(void) makeToolBars
{
UIToolbar *toolbar1 = [[[UIToolbar alloc] init] autorelease];
[toolbar1 setBarStyle:UIBarStyleBlackTranslucent];
[toolbar1 sizeToFit];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToNextField)];
UIBarButtonItem *flexButton1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *itemsArray1 = [NSArray arrayWithObjects: nextButton,flexButton1, nil];
[flexButton1 release];
[nextButton release];
[toolbar1 setItems:itemsArray1];
[txtUserName setInputAccessoryView:toolbar1];
}
The below statement is wrong.
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToNextField)];
use the below modified correct code .
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc]
initWithTitle:#"Next"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(goToNextField:)];
Because you might forget to append colon in goToNextField (Correct goToNextField:).
And in your function implementation for goToNextField you were considering the colon and put :(id) sender in your function.
You've written action:#selector(goToNextField)];. It should be action:#selector(goToNextField:)];
how can i add buttons just beside a left barButton or just beside rightbarButton,
i mean i need to have more than 2 buttons on the title bar is that possible?
thanx in advance
You can use a view with two buttons in your leftBarButton -
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftBtnsView];
As drawn from this blog post:
Think of a toolbar as your container,
instead of the UIView object. And
since a UIToolBar is UIView based, it
can be added to the right side of a
nav bar using the above trick. The
following code shows how you can add
two standard buttons to the right
side:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
Also check the SO post
Adding buttons to navigation bar
I would suggest to use UISegmentedControl to hold all your buttons and use
[[UIButton alloc] initWithCustomView:] to show the segment control.
Here is a sample of how you could go about doing this. I am using a MasterDetailView template that comes with the top navigation bar for this example:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] init];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] init];
leftButton.title = #"Left button";
rightButton.title = #"right button";
self.navigationItem.leftBarButtonItem = leftButton;
self.navigationItem.rightBarButtonItem = rightButton;
}
You can do something like this, there's something called UINavigationItem.rightBarButtonItems
UIBarButtonItem *settingsButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"] style:UIBarButtonItemStyleDone target:self action:#selector(something)];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"xxx.png"]
style:UIBarButtonItemStyleDone target:self
action:#selector(something)];
self.navigationItem.rightBarButtonItems = #[settingsButton, menuButton];
i think you need something like this
UIBarButtonItem *firstButton = [[UIBarButtonItem alloc] initWithTitle:#"First" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *secondButton = [[UIBarButtonItem alloc] initWithTitle:#"Second" style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationController.navigationItem.leftBarButtonItems=#[firstButton, secondButton];
it will add two bar button items on left of navigation bar. you can make it on right side the same way
self.navigationController.navigationItem.rightBarButtonItems=#[firstButton, secondButton];