Statement You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you. Article Source: http://blog.csdn.net/iukey |
The uisegmentedcontrol segment control replaces the single-choice button on the desktop OS. However, the number of options is very limited, because your iOS device screen is limited. It is suitable when we need to use a single button with very few options.
I. Create
Uisegmentedcontrol * mysegmentedcontrol = [[uisegmentedcontrol alloc] initwithitems: Nil];
Isn't it strange that the location and size are not specified? Yes, I did find only initwithitems in his class declaration but not initwithframe, so he does not need to specify it, But I see another method, which can set the width of the item:
Mysegmentedcontrol setwidth: 100 forsegmentatindex: 0]; // you can specify the width of an item.
Ii. Attributes
Mysegmentedcontrol. segmentedcontrolstyle = uisegmentedcontrolstylebar; // Style
You can choose from the following three styles as needed:
Typedef Enum {uisegmentedcontrolstyleplain, // large white button with gray edge in Large Plain, // small button/nav bar style. the tintable button is suitable for uisegmentedcontrolstylebezeled and large bezeled styles in the navigation bar. tintable} uisegmentedcontrolstyle;
If you are using the uisegmentedcontrolstylebar style, you can also use the tintcolor attribute of the space to set the rendering color for the entire control:
UIColor *myTint = [[ UIColor alloc]initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0]; mySegmentedControl.tintColor = myTint;
3. add and delete clips
Each segment control fragment is a button that contains a tag or image. You need to create a clip for each control in your control. As long as the screen is placed, there are many clips, but you can only select one clip at a time.
[mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:YES]; [mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:2 animated:YES];
Each
The button is assigned an index, which is used for sorting and identification.
You can also add a segment containing the image, using insersegmentwithimage
[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"] atIndex:3 animated:YES];
Delete a clip
[Mysegmentedcontrol removesegmentatindex: 0 animated: Yes]; // delete a part [mysegmentedcontrol removeallsegments]; // delete all parts
Iv. segment title
[Mysegmentedcontrol settitle: @ "zero" forsegmentatindex: 0]; // set the title nsstring * mytitle = [mysegmentedcontrol titleforsegmentatindex: 1]; // read the title
V. Images
You can also set images for each segment:
[Mysegmentedcontrol setimage: [uiimage imagenamed: @ "pic"] forsegmentatindex: 1]; // set uiimage * myimage = [mysegmentedcontrol imageforsegmentatindex: 2]; // read
Note: The image size will not be automatically adjusted, and the image size will be displayed on a large scale, so you need to notify the artist of the exact size of the image.
6. Selected segments
The default behavior of the segment control is to keep the button after it is selected until another button is selected. You can change the default behavior to be automatically released shortly after the button is pressed. Set the momentary attribute of the control to yes:
mySegmentedControl.momentary = YES;
Note: When this function is enabled, the trigger part does not update selectedsegmentedindex, so the selected part cannot be obtained through this attribute.
Initialize default segments
By default, no clips are selected unless specified. To set the selectedsegmentedindex attribute:
mySegmentedControl.selectedSegmentedIndex = 0;
7. display controls
[Parentview addsubview: mysegmentedcontrol]; // Add to parent view or self. navigationitem. titleview = mysegmentedcontrol; // Add to navigation bar
8. Read controls
You can use the selectedsegmentedindex attribute to read the value of the selected segment, which is the index number of the selected segment.
int x = mySegmentedControl. selectedSegmentedIndex;
IX. Notification
You can use the addtarget method of the uicontrol class to add an action for the uicontroleventvaluechanged event to receive the notification selected by the clip:
[mySegmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged];
If you select a clip, your action method will be called:
-(void)selected:(id)sender{ UISegmentedControl* control = (UISegmentedControl*)sender; switch (control.selectedSegmentIndex) { case 0: // break; case 1: // break; case 2: // break; default: break; }}
10. Demo
I wrote an article and tested the demo:
Uisementedcontroldemo