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.
Related
I want to add two buttons with custom image to Navigation Bar with some specific position.
I found solution But it is for Right/Left Navigation Bar Button.
My code for that is:
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 90.0f, 55.01f)];
// Add Pin button.
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(Edit:)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.width = 45;
[buttons addObject:bi1];
[bi1 release];
// Add Hot Spot button.
UIBarButtonItem *bi2 = [[UIBarButtonItem alloc] initWithTitle:#"+" style:UIBarButtonItemStylePlain target:self action:#selector(Add:)];
bi2.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi2];
[bi2 release];
// Add buttons to toolbar and toolbar to nav bar.
[tools setItems:buttons animated:NO];
[buttons release];
// Add toolbar to nav bar.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
How can i do this?
UIView *vieww =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[vieww addSubview:yourBtn1];
[vieww addSubview:yourBtn2];
[self.navigationController.navigationBar addSubview:vieww];
And if you want to remove yourButtonView then make is global object;
in .h
UIView *vieww;
and in .m
-(void)viewWillDisappear:(BOOL)animated
{
[vieww removeFromSuperview];
}
Or follow this for more Link
if you are using >iOS 5, then use this.
UIBarButtonItem *btn1=[[UIBarButtonItem alloc] initWithTitle:#" + " style:UIBarButtonItemStyleDone target:self action:#selector(action1:)];
UIBarButtonItem *btn2=[[UIBarButtonItem alloc] initWithTitle:#" - " style:UIBarButtonItemStyleDone target:self action:#selector(action2:) ];
self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:btn1,btn2,nil];
for < iOS 5 u can use following:
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 160, 44.01)];
tools.barStyle = UIBarStyleBlackOpaque;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:4];
[buttons addObject:btn1];
[buttons addObject:btn2];
[tools setItems:buttons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
Instead of adding toolbar, you can create one UIView, add two buttons on that view.
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:yourView];
If you want to do this in combination with using a storyboard, take a look at this question.
I have added array of bar button to the navigation items using the property rightBarButtonItems,it work good for iOS5,when i tested in iOS6 only one bar button item is visible.
UIBarButtonItem *updateButton = [[UIBarButtonItem alloc]
initWithTitle:#"Update"
style:UIBarButtonItemStylePlain
target:self
action:#selector(updateData)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refresh)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:updateButton,refreshButton, nil];
self.navigationItem.rightBarButtonItems=arrBtns;
Is there any new property for iOS6 to add the array of bar button to navigationitem.
Any help would be appreciated,Thanks a lot.
Please use the segmentController on the rightBarButtonItems if you want to add multiButton on rightBarButtonItems of NavigationBar
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"Add",#"Delete",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 80, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setWidth:35.0 forSegmentAtIndex:0];
[segmentedControl setWidth:45.0 forSegmentAtIndex:1];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.leftBarButtonItem = segmentBarItem;
[segmentBarItem release];
Secondly add the second button on other side of the first bar Button.
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.
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];
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];`