Programming the iPhone : Standard Control Types (part 6) - Segmented Controls

8/4/2012 3:23:39 PM

6. Segmented Controls

Segmented controls provide a compact, persistent grouping of buttons that switch between views. According to the mobile HIG, segmented controls should provide feedback to users by swapping views or otherwise appropriately updating the UI. The feedback should be immediate, avoiding animation effects. It’s conceivable to use segmented controls for complex view management. For example, the following code illustrates the use of segmented controls to manage pagination for a UIScrollView with fixed, known content. This approach isn’t standard for pagination, but it may prove to be more usable than the UIPageControl class, which uses very tiny buttons for paging:

#import "RootViewController.h"
#import "SegmentedPaginatorAppDelegate.h"

@implementation RootViewController

- (void)viewDidLoad
	UISegmentedControl *segmentedControl = [[[UISegmentedControl alloc]
											 [NSArray arrayWithObjects:
											  nil]] autorelease];
	[segmentedControl addTarget:self action:@selector(segmentChosen:)
	segmentedControl.frame = CGRectMake(0, 0, 200, 30.0);
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
	segmentedControl.momentary = NO;

	self.navigationItem.titleView = segmentedControl;
	UIScrollView *scrollView = [[UIScrollView alloc]
	scrollView.contentSize = CGSizeMake(320.0, 422.0);
	scrollView.scrollEnabled = NO;
	[scrollView setPagingEnabled:YES];

	UIView *viewOne = [[UIView alloc]
		initWithFrame:CGRectMake(0.0, 0.0, 320.0, 422.0)];
	viewOne.backgroundColor = [UIColor redColor];
	UIView *viewTwo = [[UIView alloc]
		initWithFrame:CGRectMake(320.0, 0.0, 320.0, 422.0)];
	viewTwo.backgroundColor = [UIColor blueColor];
	UIView *viewThree = [[UIView alloc]
		initWithFrame:CGRectMake(640.0, 0.0, 320.0, 422.0)];
	viewThree.backgroundColor = [UIColor greenColor];
	UIView *viewFour = [[UIView alloc]
		initWithFrame:CGRectMake(960.0, 0.0, 320.0, 422.0)];
	viewFour.backgroundColor = [UIColor orangeColor];
	UIView *viewFive = [[UIView alloc]
		initWithFrame:CGRectMake(1280.0, 0.0, 320.0, 422.0)];
	viewFive.backgroundColor = [UIColor yellowColor];

	[scrollView addSubview:viewOne];
	[scrollView addSubview:viewTwo];
	[scrollView addSubview:viewThree];
	[scrollView addSubview:viewFour];
	[scrollView addSubview:viewFive];
	scrollView.contentSize = CGSizeMake(1600.0, 422.0);

	[viewOne release];
	[viewTwo release];
	[viewThree release];
	[viewFour release];
	[viewFive release];

	self.view = scrollView;

- (void)segmentChosen:(id)sender
	UISegmentedControl* segmentedControl = sender;
	NSUInteger i = [segmentedControl selectedSegmentIndex];
	UIScrollView *scrollView = (UIScrollView *)self.view;
		scrollRectToVisible:CGRectMake((320.0 * i), 0.0, 320.0, 422.0)



Figure 10 shows the example running in the iPhone emulator.

Figure 10. Example use of segmented control for pagination

Obviously, this implementation is just a simple example. It wouldn’t be terribly difficult to develop a more usable clone of the UIPageControl element based on the UISegmentedControl class and the segmented control pattern, though such an exercise is outside the scope of this book.

Overall, working with segmented controls is simple and adds a rich supplement to your organization and navigation toolkit.
  •  Sony Introduced 3 New Phones In Xperia Series: Tipo, Miro And Ion
  •  Samsung Galaxy SIII And HTC One X: A Detailed And Thorough Comparison
  •  In Control
  •  BlackBerry Virtual Keyboard: The Development Through Each Appliance
  •  3D Phone – Why Is LG Optimus 3D Max P725 The Best Choice, Still?
  •  XNA Game Studio 4.0 : Xbox 360 Gamepad (part 2) - Moving Sprites Based on Gamepad Input
  •  XNA Game Studio 4.0 : Xbox 360 Gamepad (part 1) - Reading Gamepad State
  •  XNA Game Studio 4.0 : Adding Interactivity with User Input - Precision Control of a Mouse
  •  Asus Transformer Pad TF300
  •  Mobile Phone Game Programming : Using Sprite Animation - Building the UFO Example Program
  •  Mobile Phone Game Programming : Using Sprite Animation - Achieving Smooth Animation with the GameCanvas Class
  •  Mobile Phone Game Programming : Using Sprite Animation - Working with the Layer and Sprite Classes
  •  Windows Phone 8 Unveiled
  •  iOS 6 Beta Review (Part 2)
  •  iOS 6 Beta Review (Part 1)
  •  Ipad : Tabletop
  •  Ipad Lion (Part 3) - Other possible features
  •  Ipad Lion (Part 2) - What to expect from the upcoming mountain lion
  •  Ipad Lion (Part 1) - Lion paving the way for mountain lion's destiny
  •  The Effect Of IOS And Facebook On Shutterbugs (Part 2)
    Top 10
    HP ElitePad 900 - A Well-Built Business Tablet
    Huawei MediaPad 7 Lite Android Tablet
    Best New App-Ons – August 2013
    How To See Through Website Lies
    17 Killer Mac Apps Under $20 (Part 5)
    17 Killer Mac Apps Under $20 (Part 4)
    17 Killer Mac Apps Under $20 (Part 3)
    17 Killer Mac Apps Under $20 (Part 2)
    17 Killer Mac Apps Under $20 (Part 1)
    PC Specialist Fusion Z11 - Black Midi Tower Window Gaming Case
    Most View
    Kingston SSDNow V300 Solid State Hard Drive (Part 1)
    Do New Things With Your PC, Laptop And Gadgets (Part 1)
    The Asus Vivo Tab RT - A Surprisingly Slender And Light Device (Part 2)
    Canon Pixma Pro-10 A3+ Professional Printer (Part 1)
    Stream And Watch Your Movies Anywhere
    GoPro Hero3 Black Edition - Digital Camcorders
    Toshiba STOR.E Edition 500GB
    The Smartest OS?
    The Big News In Digital Publishing (Part 1)
    Intel’s Next Unit Of Computing Review - The Future Desktop Or Another Nettop? (Part 3)