One of the controls that we often use is tab, which helps us divide the app into several modules, but what if we want to subdivide it within one interface? This is the time to use the Uisegmentedcontrol control, the use of this control is segmented control, the implementation of the following style:
When a segment is selected, its color is filled, the remaining segments are unchecked, and it is clear that the user is currently under which segment content, and the user can easily switch.
The specific implementation, or the first look at the code:
- (void) Viewdidload {[SuperViewdidload];//Initialize, add segment name, automatically layout Self. Segmentedcontrol= [[UisegmentedcontrolAlloc] initwithitems:@[@"Selling Now", @"off Shelf"]]; Self. Segmentedcontrol. Frame= CGRectMake (-5, -, screenwidth+Ten, -);//Set the overall hue Self. Segmentedcontrol. Tintcolor= Cmgreen;//Set the font for the segment name nsdictionary*dic = [nsdictionarydictionarywithobjectsandkeys:cmgreen,nsforegroundcolorattributename,[UifontSystemfontofsize: -],nsfontattributename,Nil]; [ Self. SegmentedcontrolSettitletextattributes:dic Forstate:uicontrolstatenormal];//Set the initial check Self. Segmentedcontrol. Selectedsegmentindex=0; [ Self. SegmentedcontrolAddTarget: SelfAction@selector(SelectItem:) forcontrolevents:uicontroleventvaluechanged];//Add Response method //Set the Segmentedcontrolstyle property of the style will no longer work after iOS 7.0 //Set to reset after click, Default to No, click to remain selected Self. Segmentedcontrol. Momentary=YES;//Insert a segment at the specified ordinal and automatically layout[ Self. Segmentedcontrolinsertsegmentwithtitle:@"deleted"Atindex:2Animated:NO];//Remove a segment at the specified ordinal and automatically layout[ Self. SegmentedcontrolRemovesegmentatindex:2Animated:NO];//sets the width of the segment at the specified ordinal[ Self. SegmentedcontrolSetWidth:50.0Forsegmentatindex:1];//Gets the contents of the title at the specified ordinal NSLog(@"%@", [ Self. SegmentedcontrolTitleforsegmentatindex:1]);//Gets the width of the caption at the specified ordinal NSLog(@"%f", [ Self. SegmentedcontrolWidthforsegmentatindex:1]);//Set the segment at the specified ordinal is not selectable[ Self. SegmentedcontrolSetEnabled:NOForsegmentatindex:1]; [ Self. ViewAddsubview: Self. Segmentedcontrol];} - (void) SelectItem: (Uisegmentedcontrol*) Sender {if(Sender. Selectedsegmentindex==0) {NSLog(@"Selling Now"); }Else{NSLog(@"off Shelf"); }}
The notes are already written in a relatively clear way, with a few notes to mention in particular:
- The Uisegmentedcontrol style cannot be set after iOS 7.0, and the corresponding properties are also explained, even if the settings are not valid.
- In fact, the style of Uisegmentedcontrol a whole rounded rectangle, on both sides is not like in the picture so there is no left and right side, but I think four round angle is not very good-looking, so with a small tip, its starting X coordinate set to 5, its width is set to screen width + 10, so that the rounded corners are hidden, it seems to be beautiful, I tried to use a layer to adjust the angle to 0, does not work.
- Fonts cannot be set directly, but can only be created as a dictionary in code.
- The action that evokes the response is uicontroleventvaluechanged, a response that alters the value, not the touchupside of the button.
- Sections of the title can be used in text (I am here are text) can also use pictures, basically have a corresponding method, you can use the time according to the code to complete the hint to find.
- If the initial segment header is not set at initialization, but instead is initialized with Initwithfram, I try to use Insertsegmentwithtitle to add a caption later, which does not work.
That's one of my little superficial studies, you can download my sample project on GitHub
All rights reserved: Http://blog.csdn.net/cloudox_
IOS Uisegmentedcontrol controls in a detailed