Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
dave Avatar asked Oct 02 '09 12:10

dave


People also ask

How do I change the navigation bar on my Iphone?

A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I customize my navigation bar?

From Settings, tap Display, and then tap Navigation bar. Make sure Buttons is selected, and then you can choose your desired button setup at the bottom of the screen. Note: This option will also affect the location you swipe when using Swipe gestures.

What is segmented control in iOS?

In iOS applications, segmented controls allow you to select one of several options. Each option is represented by a segment - a button, which is part of the segmented control. The user can touch a segment to select it.

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.


2 Answers

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.

like image 111
Drew C Avatar answered Nov 02 '22 23:11

Drew C


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];
like image 38
T.J. Avatar answered Nov 03 '22 01:11

T.J.