iPhone: adding segmented control to toolbar instead of buttons within navigation controller? - iphone

im new to iphone programming so if you could help me out I would appreciate it- i have been all over the web and cant find the answer to this.
my current setup is like this
navigation controller in MainWindow.xib > View in navigation controller in MainWindow.xib calls RootViewController.xib > RootViewController.xib contains a single tableview.
i can then load in a toolbar using the following code in RootViewController.m
UIBarButtonItem *buttonOne = [[UIBarButtonItem alloc] initWithTitle:#"One"
style:UIBarButtonItemStyleBordered target:self action:#selector(buttonOnePushed)];
UIBarButtonItem *buttonTwo = [[UIBarButtonItem alloc] initWithTitle:#"Two"
style:UIBarButtonItemStyleBordered target:self action:#selector(buttonTwoPushed)];
NSArray *barArray = [NSArray arrayWithObjects: buttonOne, buttonTwo, nil];
[buttonOne release];
[buttonTwo release];
[self setToolbarItems:barArray animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
this code works for buttons. but i cannot for the life of me find out how to add a segmented control instead of the buttons. i have tried an array with two segmented controls in it, but then can't add the array to the toolbar.
if anyone could let me know some code that will add segmented controls in the same fashion as i have used to add the buttons i would greatly appreciate it.
thanks, dave.

The solution to this is to (1) create the UISegmentedControl with all its buttons, etc., and then (2) create a UIBarButtonItem using the initWithCustomView:(UIView *)view initializer and provide the segmented control as the variable to this. Then add the Bar Button Item to the Toolbar using an array just like you did in your example code.
Make sure you set a target and action for your segmented controller, and I recommend setting its style to UISegmentedControlStyleBar. It'll look just like the one at the bottom of the Maps app. Hope this was what you are looking for.

Here is my code which adds a segmented control to the toolbar of a navigation controller.
:
NSArray *segItemsArray = [NSArray arrayWithObjects: #"Settings", #"Templates", #"Notes", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 2;
UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *barArray = [NSArray arrayWithObjects: flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];
[self setToolbarItems:barArray];

Related

Arrows at the bottom of the toolbar in iOS apps

Was wondering if anyone knew how to implement arrows in the bottom toolbar as shown below? Would be extremely grateful if someone could give me insight into this. I have seen a lot of apps with such arrows to navigate through pages and cannot seem to find them in XCode. See below...
THX :)
Just set a UIToolbar and attach it those two images (i'll give you the archive link with both images standard and retina): link
//Creating UIToolbar
toolBar = [[UIToolbar alloc]init];
toolBar.frame = CGRectMake(//set some frame);
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
//Creating buttons
UIBarButtonItem *prev = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"previous.png"] style:UIBarButtonItemStylePlain target:self action:#selector(myaction:)] autorelease];
UIBarButtonItem *next = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"next.png"] style:UIBarButtonItemStylePlain target:self action:#selector(myaction:)] autorelease];
//Creating the array with the buttons
NSArray *items = [NSArray arrayWithObjects:prev,next, nil];
//Assigning the array to the toolbar
[toolBar setItems:items];
[self.view addSubview:toolBar];
[toolBar release];
You are done!
I'm guessing those arrows were custom made by Apple for Safari, Photo Library, etc.
I think you can make those in Paint if you don't have something along the lines of Photoshop.

Centering UIBarButtonItem in a UIToolbar and adding custom text

I need to add a button to the center of the ToolBar. I have done the adding the button to the toolbar part successfully. My problems are as follows;
1.) I need to center this barbutton. It should be in the center of the Tool Bar
2.) I need to have a text after the refresh button image is displayed.
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
NSMutableArray* button = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
[button addObject:barButton];
[toolBar setItems:button animated:NO];
[self.view addSubview:toolBar];
1. Add flexible spacers before and after your bar button in the toolbar items array:
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0 , 320 , 60)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshButtonAction:)];
NSArray *toolbarItems = [NSArray arrayWithObjects:flexibleSpace, barButton, flexibleSpace];
[toolBar setItems:toolbarItems animated:NO];
[self.view addSubview:toolBar];
Configuring toolbars is much easier to do in Interface Builder. If your view controller is inside a UINavigationController stack, you can still use IB to create an outlet collection of UIBarButtonItems and set self.toolbarItems in -viewDidLoad.
2. To get custom content in a toolbar you can create a bar button item with a custom view:
UIView *customView = <# anything, could be a UILabel #>;
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
I know this can be done in IB, but I believe if you want to center a button, you will need to add a fixed or flexible space button on either side to keep your button in the middle. If you are going to do this with just code... try and sandwich your button between the 2 space buttons.

How can i add a next and previous button at the segmented Controller on a navigation bar in iphone application development?

I am in great trouble....How can i set next and previous button/arrow at my segmented bar...if anyone need brief about my problem then please see this link...How can i add a next and previous button at the segmented Controller?
i have attached an image to understand the problem...so anybody help me please....
NOTE THAT: In my current project it has more than 5 buttons to add at the segmented bar so when i will press next/previous arrow then segmented bar should be move from his place.If my question is not clear to you then please see my another link....
Thanks in Advance
EDIT:
UIBarButtonItem *previousBarButtonItem = [[UIBarButtonItem alloc] //init];
initWithTitle:#"<"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(previousBarButtonAction:)];
self.navigationItem.leftBarButtonItem = previousBarButtonItem;
[previousBarButtonItem release];
UIBarButtonItem *nextBarButtonItem = [[UIBarButtonItem alloc] //init];
initWithTitle:#">"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(nextBarButtonAction:)];
self.navigationItem.rightBarButtonItem = nextBarButtonItem;
[nextBarButtonItem release];
//This Portion For UIToolbar
topToolBar = [UIToolbar new];
topToolBar.barStyle = UIBarStyleDefault;
[topToolBar sizeToFit];
topToolBar.frame = CGRectMake(50, 410, 280, 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
[topToolBar setItems:items animated:NO];
self.navigationItem.titleView = topToolBar;
this is my current coding position now i have 4 buttons in the uitoolbar but only 3 button can see so i want to move this toolbar when i will press next or previous button to see the others button whose are out of frame of uitoolbar??
EDIT:
I able to scroll the navigation bar item using uiview animation but now my problem is when i press the next/prev button then it is moving from the current place according to the changing of the coordinate of the uitoolbar and moving over the pre/next baritem frame whose are not in the uitoolbar items. but it should be wothin a uiview and should change the coordinate within the uiview not out of the view...now tell me what can i do for this problem.
Firstly in figure the NavigationBar you are seeing is actually UIToolBar. Unfortunately it is not possible to add anymore controls on the UINavigationBar. But you can achieve exactly same UI with UIToolBar where you can add any controls.
So to achieve this use UIToolBar and not UINavigationBar. Also use UIBarButtonItem with custom title to achieve Next and Previous functionality.
EDIT
Here are few links for example of UIToolBar
http://www.codeproject.com/Articles/43658/How-to-Make-a-Toolbar-with-UIToolbar
http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/
But this all explains using codes. Instead using Interface Builder, it becomes too easy to use UIToolBar (if coding is not so important).
I think this is being slightly overcomplicated- can you not just set the text of the two UIBarButtonItems to "<" and ">"?

How to center UISegmentControl in a toolbar at top of UIPopoverController

I've seen a few different posts on this, but I can't seem to get it working. I basically have a UITableView and want sort buttons at the top of a popover controller. I followed this post: UIPopoverController toolbar at top in order to get started. In my controller that is the rootViewController of the navigationController, I can create a UISegmentControl and place it at the top. However, it does not look like the picture in that it's not centered. Maybe because the way I get it into the popover is in the viewDidLoad of the popover like this:
UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"One", #"Two", #"Three", #"Four", nil]];
topSegmentControl.backgroundColor = [UIColor clearColor];
topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
// UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
self.navigationItem.leftBarButtonItem = toolBarCustom;
Also, if I want to present data at the bottom of the popovercontroller in a toolbar, I'm not sure where to do that. Following the same example: UIPopoverController toolbar at top, I thought in my navigationController, I would do something like this:
UISegmentedControl *topSegmentControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"BottomOne", #"BottomTwo", #"BottomThree", nil]];
// topSegmentControl.backgroundColor = [UIColor clearColor];
topSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem *toolBarCustom = [[UIBarButtonItem alloc] initWithCustomView:topSegmentControl];
UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *array = [NSArray arrayWithObjects:spaceItem, toolBarCustom, spaceItem, nil];
[navController setToolbarItems:toolBarCustom];
[navController setToolbarHidden:NO];
When I try this, I see a toolBar with nothing in it, that is a lighter tint than the rest of the popover.
To summarize, I'm not sure as to where you would initialize toolbar or barbuttonitems for a popover that has a navigationcontroller like in the example. I'm also not sure how to center the data. Thanks.
You’ll probably be best suited by just setting your UISegmentedControl as the titleView of your root view controller’s navigationItem.

Wrong UIToolbar Appears After Popping View

I'm new to Objective-C/Cocoa Touch and hope someone can help with the following issue.
I've got a view with a mapview control and a UIToolBar at the bottom of the screen. When a user clicks a pin on the map they can drill down to a grouped UITableView with details of the location.
This is done by pushing a new navigationController:
MapAnnotation *tappedLocation = (MapAnnotation *)[view annotation];
LocationDetailsViewController *placeDetails = [[LocationDetailsViewController alloc] initWithNibName:#"LocationDetailsViewController" bundle:nil];
[self.navigationController pushViewController:placeDetails animated:YES];
[placeDetails release];
In the grouped UITableView there's also a UIToolbar at the bottom of the screen, but it's set up programmatically (code below).
The issue is that when the user leaves the grouped UITableView and comes back to the main view with the map, they still see the UIToolbar from the UITableView, not the original one that was set up in IB.
Anyone know how I can 'cancel' the UIToolbar from the grouped UITableView?
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 436, 320, 44);
toolbar.tag = 123;
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *emailButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(email:)];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: flexItem, flexItem, flexItem, emailButton, nil];
[emailButton release];
[flexItem release];
//add array of buttons to toolbar
[toolbar setItems:items animated:YES];
[self.navigationController.view addSubview:toolbar];
Don't add the toolbar as a subview of the navigationController. That view stays on screen as long as the app is running (you'll be stacking up a lot of them if you keep on going into the details view and back).
Simply set the toolbar's items using [UIViewController setToolbarItems:animated:]