IOS learning notes (7) Basic concepts and usage of UISegmentedControl segment controls (blog address: http://blog.csdn.net/developer_jiangqq)
Author: hmjiangqq
Email: jiangqqlmj@163.com
UISegmentedControl
First, let's take a look at the official website introduction:
AUISegmentedControl
Object is a horizontal control made of multiple segments, each segment functioning as a discrete button. A segmented control affords a compact means to group together a number of controls.
A segmented control can display a title (NSString
Object) or an image (UIImage
Object).UISegmentedControl
Object automatically resizes segments to fit proportionally within their superview unless they have a specific width set. When you add and remove segments, you can request that the action be animated with sliding and fading effects.
The segment control is a selection control, which is similar to the single-choice button in Windows and consists of two ends or more segments,
Each segment is equivalent to an independent button. This control generally has two styles: Plain & Bordered style and Bar style,
The Bordered style adds a border on the Plain style. Plain and Bordered style. You can set text for each section.
It is relatively narrow compared with adding an image. Bar style. We generally do not place images in it.
Common attributes and methods are as follows:
The instance code is as follows:
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease]; // Override point for customization after application launch. self. window. backgroundColor = [UIColor whiteColor]; NSArray * array = @ [@ "Search", @ "select", @ "video", @ "image"]; UISegmentedControl * segmentControl = [[UISegmentedControl alloc] initWithItems: array]; segmentControl. segmentedControlStyle = UISegmentedControlStyleBordered; // you can specify the segmentControl position. frame = CGRectMake (60,100,200, 40); // segmentControl is selected by default. selectedSegmentIndex = 1; // sets the background color segmentControl. tintColor = [UIColor greenColor]; // sets the listening event [segmentControl addTarget: self action: @ selector (change :) forControlEvents: UIControlEventValueChanged]; [self. window addSubview: segmentControl]; [segmentControl release]; [self. window makeKeyAndVisible]; return YES;}-(void) change :( UISegmentedControl *) segmentControl {NSLog (@ "segmentControl % d", segmentControl. selectedSegmentIndex );}
Run: