Directly on the code:
creation of//uisegmentedcontrol objects The //parameter array is an array of strings that represent the headings of each segment, and the number of objects in the array determines the number of segments. Uisegmentedcontrol*segmentcontrol = [[UisegmentedcontrolAlloc] initwithitems:@[@"Yellow", @"Red", @"(~"~) ~zz ", @"?"]]; Segmentcontrol. Frame= CGRectMake ( -, -, Cgrectgetwidth ( Self. View. Bounds) - -, +);//Set subscript for selected segmentsSegmentcontrol. Selectedsegmentindex=1;//When the default selected subscript is set, in order to be able to respond to the method, you need to call the corresponding method manually, and the current segmented space object as a parameter into the method, the inside of the method will be based on the sub-control of the selected subscript to make the operation. [ SelfHandlesegmentaction:segmentcontrol];//modify its appearance color by setting the surface shading of the segmented control, and the selected color changes as the border color changesSegmentcontrol. Tintcolor= [UicolorBlackcolor];//Background color//Segmentcontrol.backgroundcolor = [Uicolor browncolor]; //When adding a picture to a segment, you need to change the render mode of the image object to the original rendering mode, otherwise the picture will follow the control's tintcolor into a solid color block. UIImage*firstimage = [[UIImageimagenamed:@"2"] imagewithrenderingmode:uiimagerenderingmodealwaysoriginal];//Set the picture for the specified segmented subscript[Segmentcontrol setimage:firstimage Forsegmentatindex:0];//Bind response method[Segmentcontrol AddTarget: SelfAction@selector(handlesegmentaction:) forcontrolevents:uicontroleventvaluechanged]; [ Self. ViewAddsubview:segmentcontrol]; [Segmentcontrol release];-(void) Handlesegmentaction: (Uisegmentedcontrol*) Sender {Switch(Sender. Selectedsegmentindex) { Case 0: Self. View. BackgroundColor= [UicolorYellowcolor];NSLog(@"Yellow"); Break; Case 1: Self. View. BackgroundColor= [UicolorRedcolor];NSLog(@"Red"); Break; Case 2: Self. View. BackgroundColor= [UicolorBluecolor];NSLog(@"Blue"); Break; Case 3: Self. View. BackgroundColor= [UicolorGreencolor];NSLog(@"Green"); Break; }NSLog( @'%s ', __function__);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use of Uisegmentedcontrol