User Experience Coding How-To's
User Experience Coding How-To's |
GeneralHow do I create my user interface?In general, there are three application styles for iPhone OS: productivity, utility, and immersive. Each style has some common approaches to user interface design, which you can learn about in detail by looking at iPhone Human Interface Guidelines. When you are ready to start your application, take a look at the templates that Xcode offers and choose the one that matches your application's design. The fastest way to create your user interface is to use Apple's visual tool, Interface Builder. In it you can assemble your application’s user interface graphically, and then programmatically create any user interface elements not offered by the tool. You can see some examples of how to create a variety of user interface elements by looking at these sample applications:
For comprehensive information on the classes and methods for creating individual elements, see UIKit Framework Reference. How do I start my application in landscape mode?To start your application in landscape mode, edit your <key>UIInterfaceOrientation</key> <string>UIInterfaceOrientationLandscapeRight</string> For detailed information see "Launching in Landscape Mode" in iPhone Application Programming Guide. How do I enable autorotation?To use autorotation, you must use a view controller (and subclass it), and the subclass must return -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return YES; } You can also choose whether to allow or deny the rotation, returning You must create the window with the size You can also enable support for autorotation in Interface Builder:
If you are also using a toolbar, the view controller for each toolbar item must implement the How do I flip my interface, as the Weather application does?To "flip" your user interface, use the See the UICatalog sample application and UIView Class Reference. The "Utility Application" template in Xcode provides a starting point for implementing applications which, like the Weather application, have a "front" and a "back" view. How do I handle autorotation of my user interface?For your application to support autorotation, where your interface changes between portrait and landscape orientation, it needs to implement a How do I internationalize or localize my application's text strings?To internationalize your application's text strings, you store your localized text in You can then obtain the localized text in the user's currently selected language for display using the For example, to display the French word for "City" in your interface, you would first add an entry to your "City" = "Ville"; label.text = NSLocalizedString(@"City", @"Label for City text field"); You can also define the localizable strings in your nib file, and create localized copies of that nib. See "Internationalizing Your Application" in iPhone Application Programming Guide for more information. | ||
Multi-TouchHow do I handle touch-based input such as tracking the user's finger or detecting multiple touches?To receive touch input, you instantiate a You can implement separate handlers for the different "phases" of a touch—when touches begin, move, end, or are canceled by the system. See Listing 5 for the available set of handlers. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; By default, a view ignores all but the first touch during a multi-touch sequence. If you want the view to handle multiple touches you must enable multiple touches for the view. This can be done programmatically by setting the multipleTouchEnabled property of your view to YES, or in Interface Builder using the inspector for the related view. For detailed information, see "Event Handling" in iPhone Application Programming Guide and the UITouch Class Reference. For an example of how to handle touch input, see the MoveMe sample application. | ||
Windows, Views, and ControlsHow do I detect the screen boundary?To detect the screen boundary, use the How do I create a window?To create a window, use the window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; For more information, see UIWindow Class Reference. How do I create a window with a navigation controller?To create a window with a navigation controller, use the self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; [window setBackgroundColor:[UIColor redColor]]; MainViewController *navController = [[MainViewController alloc] init]; // create a navigation controller using the new controller navigationController = [[UINavigationController alloc] initWithRootViewController:navController]; [navController release]; // Add the navigation controller's view to the window [window addSubview:[navigationController view]]; [window makeKeyAndVisible]; For more information, see UINavigationController Class Reference. How do I create a button?To create a button, use the CGRect frame = CGRectMake(0.0, 0.0, 100.0, 40.0); UIImage *buttonImage = [UIImage imageNamed:@"grayButton.png"]; UIButton *stopButton = [[UIButton alloc] initWithFrame:frame]; [stopButton setTitle:@"Button" forState:UIControlStateNormal]; [stopButton setTitleColor:[UIColor blackColor] forState:UIControlEventTouchDown]; [stopButton setBackgroundImage:buttonImage forState:UIControlStateNormal]; stopButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; stopButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; [stopButton addTarget:self action:@selector(stopAction:) forControlEvents:UIControlEventTouchUpInside]; [stopButton setBackgroundColor:[UIColor clearColor]]; // then add the button to a UIView like this: // [myView addSubview: stopButton]; For more information, see UIButton Class Reference. How do I create a label?To create a label, use the CGRect labelFrame = CGRectMake(0.0, 0.0, 100.0, 30.0); UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; [label setFont:[UIFont systemFontOfSize:12]]; label.textAlignment = UITextAlignmentLeft; [label setText:@"TextLabel:"]; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor blackColor]; [self.view addSubview:label]; For more information, see UILabel Class Reference. How do I create a switch?To create a switch, use the CGRect frame = CGRectMake(0.0, 0.0, 60.0, 26.0); UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame]; [switchControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; [switchControl setBackgroundColor:[UIColor clearColor]]; For more information, see UISwitch Class Reference. How do I create a text field?To create a text field, use the CGRect textFieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0); UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame]; [textField setBorderStyle:UITextFieldBorderStyleBezel]; [textField setTextColor:[UIColor blackColor]]; [textField setFont:[UIFont systemFontOfSize:20]]; [textField setDelegate:self]; [textField setPlaceholder:@"<enter text>"]; [textField setBackgroundColor:[UIColor whiteColor]]; textField.keyboardType = UIKeyboardTypeDefault; For more information, see UITextField Class Reference. How do I create a text view?To create a text view, use the CGRect textFieldFrame = CGRectMake(0.0, 0.0, 200.0, 200.0); UITextView *textView = [[UITextView alloc] initWithFrame:textFieldFrame]; [textView setTextColor:[UIColor blackColor]]; [textView setFont:[UIFont systemFontOfSize:20]]; [textView setDelegate:self]; [textView setBackgroundColor:[UIColor whiteColor]]; textView.keyboardType = UIKeyboardTypeDefault; For more information, see UITextView Class Reference. How do I create a web view?To create a web view, use the CGRect webFrame = CGRectMake(0.0, 0.0, 200.0, 200.0); UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; [webView setBackgroundColor:[UIColor whiteColor]]; For more information, see UIWebView Class Reference. How do I create a view for images?To create a view for images, use the UIImage *image = [UIImage imageNamed:@"image.png"]; UIImageView *theImageView = [[UIImageView alloc] initWithImage:image]; For more information, see UIImageView Class Reference. How do I create a slider?To create a slider, use the CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0); UISlider *slider = [[UISlider alloc] initWithFrame:frame]; [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; [slider setBackgroundColor:[UIColor clearColor]]; slider.minimumValue = 0.0; slider.maximumValue = 50.0; slider.continuous = YES; slider.value = 25.0; For more information, see UISlider Class Reference. How do I create a segmented control?To create a segmented control, use the NSArray *arrayOfImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"segment1_Image.png"], [UIImage imageNamed:@"segment2_Image.png"], [UIImage imageNamed:@"segment3_Image.png"], [UIImage imageNamed:@"segment4_Image.png"], nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems: arrayOfImages]; CGRect frame = CGRectMake(0.0, 120.0, 300.0, 44.0); [segmentedControl setFrame:frame]; [segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged]; For more information, see UISegmentedControl Class Reference. How do I create a page control?To create a page control, use the CGRect frame = CGRectMake(0.0, 0.0, 200.0, 40.0); UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:frame]; [pageControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]; [pageControl setBackgroundColor:[UIColor clearColor]]; pageControl.numberOfPages = 10; For more information, see UIPageControl Class Reference. How do I create a table view?To create a table view, use the UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; tableView.rowHeight = 54; [tableView reloadData]; For more information, see UITableView Class Reference. See also the TableViewSuite sample application. How do I add buttons to the UINavigationController?If your application has a // add our custom add button as the nav bar's custom right view UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)]; self.navigationItem.customRightItem = addButton; [addButton release]; For more information, see UINavigationController Class Reference. How do I display an alert view, action sheet, or modal view?Alerts, action sheets, and modal views are types of views that appear when something requires the user's attention or when an application needs to offer additional choices or functionality to the user. iPhone Human Interface Guidelinesprovides examples of what each view looks like, along with guidelines for when to use each type. Use the Use the Use the You can see examples of how to implement several of these views by looking at the UICatalog sample application. | ||
Document Revision History
Posted: 2009-04-30 | ||||||||||||
Back to Top |