Principle:
1. Use the Tintcolor property to set the entire uisegmentcontrol as a transparent color.
2. Set the titletextattributes in the normal state and the titletextattributes in the selected state.
#import "SecondViewController.h"#import "Masonry.h"@interfaceSecondviewcontroller () @property (nonatomic, strong) Uisegmentedcontrol*Segmentedcontrol_one;@end@implementationSecondviewcontroller#pragmaMark-Life cycle#pragmaMark Viewdidload-(void) viewdidload{[Super Viewdidload]; [Self basicsetting]; [Self addsegmentedcontrol_one]; }#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=@"Hide Border";}- (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 ( +); }];}#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]; //Remove the color, now the whole segment I can not see, the corresponding Click eventSelf.segmentedControl_one.tintColor =[Uicolor Clearcolor]; //under normal conditionNsdictionary * normaltextattributes = @{nsfontattributename: [Uifont systemfontofsize:16.0f],nsforegroundcolorattributename: [Uicolor Graycolor]}; [Self.segmentedcontrol_one settitletextattributes:normaltextattributes Forstate:uicontrolstatenormal]; //In the selected stateNsdictionary * selctedtextattributes = @{nsfontattributename: [Uifont boldsystemfontofsize:20.0f],nsforegroundcolorattributename: [Uicolor Redcolor]}; [Self.segmentedcontrol_one settitletextattributes:selctedtextattributes forstate:uicontrolstateselected]; [Self.segmentedcontrol_one addtarget:self Action: @selector (segmentedcontrol_one:) forcontrolevents:uicontrolevent ValueChanged]; } return_segmentedcontrol_one;}@end
ios-uisegmentedcontrol-Hide Border