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.
Related
I've never added a segmented control to a bottom toolbar without Interface Builder. I added the same segmented control to the navigation control toolbar without this issue. What am I doing wrong? Thank you.
//enable UIToolbar
self.navigationController.toolbarHidden = NO;
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:#"Day One",#"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:#selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:0];
[self.segmentedCon setWidth:85.0 forSegmentAtIndex:1];
[self.segmentedCon setFrame:[self.navigationController.toolbar bounds]];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
//nsarray of tool bar items
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibaleSpaceBarButton,segBarBtn,flexibaleSpaceBarButton,nil];
[self setToolbarItems:toolbarItems animated:YES];
[segBarBtn release];
Try this instead..
//UISegmentedControl
NSArray *segmentedControlItems = [[NSArray alloc] initWithObjects:#"Day One",#"Day Two",nil];
self.segmentedCon = [[UISegmentedControl alloc] initWithItems:segmentedControlItems];
[segmentedControlItems release];
[self.segmentedCon addTarget:self action:#selector(daySegConIndexChange) forControlEvents:UIControlEventValueChanged];
[self.segmentedCon setSelectedSegmentIndex:0];
[self.segmentedCon setSegmentedControlStyle:UISegmentedControlStyleBar];
//bar button items
UIBarButtonItem *flexibaleSpaceBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *segBarBtn = [[UIBarButtonItem alloc] initWithCustomView:self.segmentedCon];
Try setting the frame of the segmented control...
CGRect r = self.segmentedCon.frame;
self.segmentedCon.frame = CGRectMake(r.origin.x, r.origin.y, r.size.width, 30);
Here is my code. This is adding image but images are being appeared in navigation bar.
UIToolbar* toolbartop = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttonstop = [[NSMutableArray alloc] initWithCapacity:5];
UIBarButtonItem *remainderButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(LoadOption:)];
[buttonstop addObject:remainderButton];
[remainderButton release];
UIBarButtonItem *faveButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
[buttonstop addObject:faveButton];
[faveButton release];
// put the buttons in the toolbar and release them
[toolbartop setItems:buttonstop animated:NO];
[buttonstop release];
// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbartop];
But this code is showing image in navigation right bar.
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
self.navigationItem.rightBarButtonItem = settingButton;
Why left navigation bar button is not showing images?
[toolbartop release];
You can add the image to your left BarButtonItem as follows:
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:#selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(105,0,105,30);
[button2 setBackgroundImage:[UIImage imageNamed: #"image2.png"] forState:UIControlStateNormal];
[button2 addTarget:appDelegate action:#selector(Open_Link2) forControlEvents:UIControlEventTouchUpInside];
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 30)];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
[button1 release];
[button2 release];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
Here please make sure that the methods "Open_Link1" and "Open_Link2" must exists in the code as follows:
-(void)Open_Link1
{
// Write your logic
}
-(void)Open_Link2
{
// Write your logic
}
Let me know if you want more help.
The left bar button item is automatically used by the framework for showing a "Back" button. You can't put anything here, at least not when your navigation bar is managed by a UINavigationController. For this to work you have to handle the UINavigationBar yourself.
Edit:
in your leftBarButtonItem, try putting a UISegmentedControl instead of a UIToolbar for toolbartop.
You are adding a button to toolbar but you aren't adding your toolbar to any view. Try to set the button directly using
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];
How to Create multiple bar button in navigation bar?
From iOS 5 onwards, you can now do it using setLeftBarButtonItems:animated: or setRightBarButtonItems:animated:
You must use UIToolbar and set the toolbar with buttons:
// create a toolbar where we can place some buttons
UIToolbar *toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
you have to create a view with as much button you required and have to add them on navigation button like following :
UIView *parentView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
UIButton *infoButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 6, 30, 32)];
[infoButton1 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton1 setTitle:#"Back" forState:UIControlStateNormal];
infoButton1.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton1.titleLabel.textColor = [UIColor whiteColor];
[infoButton1 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton1];
[infoButton1 release];
UIButton *infoButton2 = [[UIButton alloc] initWithFrame:CGRectMake(30, 6, 30, 32)];
[infoButton2 setBackgroundImage:[UIImage imageNamed: #"navbtn.png"] forState:UIControlStateNormal];
[infoButton2 setTitle:#"Back" forState:UIControlStateNormal];
infoButton2.titleLabel.font = [UIFont systemFontOfSize:13.0f];
infoButton2.titleLabel.textColor = [UIColor whiteColor];
[infoButton2 addTarget:self action:#selector(backBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[parentView1 addSubview:infoButton2];
[infoButton2 release];
UIBarButtonItem *customBarButtomItem1 = [[UIBarButtonItem alloc] initWithCustomView:parentView1];
[parentView1 release];
self.navigationItem.leftBarButtonItem = customBarButtomItem1;
[customBarButtomItem1 release];`enter code here`
I know this question was already closed, but I find that the UIToolbar solution doesn't match visually.
If you instead use a second UINavigationBar set with a UINavigationItem that has a title of nil and the desired buttons you can add more buttons and have a bar that visually matches the original.
For iOS7 and higher, this is the right way to do it. No need for UIToolbar silliness.
- (void)viewDidLoad {
[super viewDidLoad];
[self configureView];
// create three funky nav bar buttons
UIBarButtonItem *one = [[UIBarButtonItem alloc]initWithTitle:#"One" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *two = [[UIBarButtonItem alloc]initWithTitle:#"Two" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
UIBarButtonItem *three = [[UIBarButtonItem alloc]initWithTitle:#"Three" style:UIBarButtonItemStylePlain target:self action:#selector(testMethod)];
// create a spacer
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
space.width = 30;
NSArray *buttons = #[one, space, two, space, three];
self.navigationItem.rightBarButtonItems = buttons;
}
I hate putting links as answers on SO as they can die anytime so i added relevant code taken from HERE
- (void)viewWillAppear
{
// get a view and :
[self.navigationController.navigationBar addSubView:yourView];
}
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];`
I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?
You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.
This is what is used in the Mail application to go to next/previous message.
Update
In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):
- (void)viewDidLoad
{
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
}