How to center UISegmentControl in a toolbar at top of UIPopoverController - iphone

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.

Related

add navigation bar programmatically to a tableviewController (segue push or modal )

I'm trying to programmatically add a navigationBar to a tableViewController :
When I come from a PUSH Segue the builder adds a navigation bar with navigation items, like I want.
But the problem is that when i come form a MODAL Segue I try to add a bar with code like this:
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, -260, 320, 44)];
[self.view addSubview:naviBarObj];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancelButtonPressed)];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonPressed)];
UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navigItem.rightBarButtonItem = doneItem;
navigItem.leftBarButtonItem = cancelItem;
naviBarObj.items = [NSArray arrayWithObjects: navigItem,nil];
....
- (void) cancelButtonPressed { }
- (void) doneButtonPressed { }
But nothing appears... No top bar.
naviBarObj's frame ypoint is -260, maybe this is making nothing appear.

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.

Buttons don't appear on the toolbar

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.

segmentedcontrol in navigationbar

I am trying to put this in a navigationbar, but doesnt show up, can u
have a look at it?
UISegmentedControl *seg1 = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"von mir", #"alle", nil]];
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:seg1];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
[self.navigationController.navigationBar setItems:[NSArray
arrayWithObjects:flexItem, barItem, flexItem, nil]];
[flexItem release];
[barItem release];
[seg1 release];
UINavigationBar's items property only accepts an array of UINavigationItem objects, not UIBarButtonItem objects. You can't configure a navigation bar the same way as you do a UIToolbar. Instead, in your view controller, do this:
UISegmentedControl * seg1 = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"von mir", #"alle", nil]];
[seg1 setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = seg1;
This adds the segmented control to the title view of your view controller's navigation item, which is a custom view that appears centered on the navigation bar.

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

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