Can be copied directly using
#import "FirstViewController.h"#import "Masonry.h"@interfaceFirstviewcontroller () @property (nonatomic, strong) Uisegmentedcontrol*Segmentedcontrol_one;@end@implementationFirstviewcontroller#pragmaMark-Life cycle#pragmaMark Viewdidload-(void) viewdidload{[Super Viewdidload]; [Self basicsetting]; [Self addsegmentedcontrol_one]; [Self Insertordeleteinsegmendtedcontrol]; [Self settingsegmentproperty]; [Self Obtainsectionitem];}#pragmaMark-System Agent#pragmaMark-click Event-(void) Segmentedcontrol_one: (Uisegmentedcontrol *) sender{NSLog (@"Index:%ld",(Long) sender.selectedsegmentindex); }#pragmaMark-Implementation Method#pragmaMark Basic Settings-(void) basicsetting{Self.title=@"Basic Concepts";}- (void) addsegmentedcontrol_one{[Self.view AddSubview:self.segmentedControl_one]; [Self.segmentedcontrol_one mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.mas_equalTo (Self.view). With.offset (Ten); Make.right.mas_equalTo (Self.view). With.offset (-Ten); Make.top.mas_equalTo (Self.view). With.offset ( -); Make.height.mas_equalTo ( +); }];}- (void) insertordeleteinsegmendtedcontrol{//insert a paragraph of text[Self.segmentedcontrol_one Insertsegmentwithtitle:@"the inserted element"Atindex:2Animated:yes]; //Insert Picture[Self.segmentedcontrol_one insertsegmentwithimage:[[uiimage imagenamed:@"1"] Imagewithrenderingmode:uiimagerenderingmodealwaysoriginal] Atindex:3Animated:yes]; //Delete[Self.segmentedcontrol_one Removesegmentatindex:0Animated:yes]; //Modify the segment title according to subscript[Self.segmentedcontrol_one Settitle:@"Modified segment Title"Forsegmentatindex:4];}- (void) settingsegmentproperty{//set the width of a paragraph[Self.segmentedcontrol_one setwidth:40.0fForsegmentatindex:2]; //sets the position (0,0) of the title in the segment as the location of the center point[Self.segmentedcontrol_one Setcontentoffset:cgsizemake (Ten,Ten) Forsegmentatindex:3];}- (void) obtainsectionitem{// //gets the picture Imageforsegmentatindex for the specified index option:Uiimageview *imageforsegmentatindex = [[Uiimageview alloc]initwithimage:[self.segmentedcontrol_one Imageforsegmentatindex:2]]; Imageforsegmentatindex.frame=cgrectmake (60.0,100.0,30.0,30.0); //gets the title of the specified index option TitleforsegmentatindexUILabel *titleforsegmentatindex = [[UILabel alloc]initwithframe:cgrectmake (60.0,200.0,200.0,30.0)]; Titleforsegmentatindex.text= [Self.segmentedcontrol_one Titleforsegmentatindex:0]; //gets the total number of options segmentedcontrol.numberofsegmentsUILabel *numberofsegments = [[UILabel alloc]initwithframe:cgrectmake (60.0,300.0,30.0,200.0)]; Numberofsegments.text= [NSString stringWithFormat:@"%lu", (unsignedLong) self.segmentedControl_one.numberOfSegments]; //gets the width of the specified index option widthforsegmentatindex:UILabel *widthforsegmentatindex = [[UILabel alloc] Initwithframe:cgrectmake (40.0,400.0,200.0,30.0)]; Widthforsegmentatindex.text= [NSString stringWithFormat:@"Width:%f", [Self.segmentedcontrol_one Widthforsegmentatindex:2]]; //Set the default selection indexSelf.segmentedControl_one.selectedSegmentIndex =2; //Set ColorSelf.segmentedControl_one.tintColor =[Uicolor Redcolor]; Self.segmentedControl_one.momentary= NO;//sets whether to revert to the original after clicking[Self.segmentedcontrol_one setenabled:no forsegmentatindex:4];//Setting the specified index option is not selectableBOOL Enableflag = [Self.segmentedcontrol_one isenabledforsegmentatindex:4];//determines whether the specified index option is optionalNSLog (@"Enableflag:%d", Enableflag); [Self.view Addsubview:widthforsegmentatindex]; [Self.view addsubview:numberofsegments]; [Self.view Addsubview:titleforsegmentatindex]; [Self.view Addsubview:imageforsegmentatindex]; [Self.view AddSubview:self.segmentedControl_one];}#pragmaMark-setter & getter-(Uisegmentedcontrol *) segmentedcontrol_one{if(!_segmentedcontrol_one) {Nsarray* Array = @[@"First paragraph",@"second paragraph",@"Third paragraph",@"Fourth paragraph"]; Self.segmentedcontrol_one=[[Uisegmentedcontrol alloc] initwithitems:array]; [Self.segmentedcontrol_one addtarget:self Action: @selector (Segmentedcontrol_one:) forControlEvents: Uicontroleventvaluechanged]; } return_segmentedcontrol_one;}@end
ios-segmented controller-Basic concepts