iOS Learning segmented control

Source: Internet
Author: User

Reprint please indicate the source

http://blog.csdn.net/pony_maggie/article/details/27086877


Author: Pony


What is segmented control? Let's start with a few pictures:




These pictures are the typical segmented control UI view, the first one is a game program, the red box is Segmentedcontrol. The back three is a demo sample of my blog post.

Segmented control has for example the following several characteristics:

1 is typically used in a single view, and does not switch between long views. Implement a different display in the view of the high-speed switch, each cut represents a different display, these displays are often related, so-called correlation, can be understood as function, but attribute categories are different, such as the game program in a few cutting.

More often than not, for example, in a view, different cutting control tableview load different data sources.

2 It is usually in the upper part of the entire screen, not necessarily, but in most cases this is used.

3 is usually 3 to 5 cuts, more than 5 words each cut of the size of the user touch experience will be very poor.

4 random moments, only one cut is active. Sort of like a radio button.

Development environment:

Mac OS +xcode5.0 + iOS7 simulator.

Build the control with code such as the following:

-(void) initsegmentedcontrol{nsarray *segmenteddata = [[Nsarray alloc]initwithobjects:@ "Apple", @ "Orange", @ "banana",    NIL];    Uisegmentedcontrol *segmentedcontrol = [[Uisegmentedcontrol alloc]initwithitems:segmenteddata];    Segmentedcontrol.frame = CGRectMake (10.0, 20.0, 300.0, 30.0); /* This is the color set when the button is pressed */Segmentedcontrol.tintcolor = [Uicolor colorwithred:49.0/256.0 green:148.0/256.0 Blu    e:208.0/256.0 Alpha:1]; Segmentedcontrol.selectedsegmentindex = 0;//The default selected button Index/* The following code is controlled by the properties of the normal state and the pressed state, such as the size and color of the font, etc. */Nsdictiona Ry *attributes = [nsdictionary dictionarywithobjectsandkeys:[uifont boldsystemfontofsize:12],nsfontattributename,[    Uicolor Redcolor], nsforegroundcolorattributename, nil];            [Segmentedcontrol settitletextattributes:attributes Forstate:uicontrolstatenormal]; Nsdictionary *highlightedattributes = [nsdictionary dictionarywithobject:[uicolor RedColor] ForKey:        Nsforegroundcolorattributename]; [Segmentedcontrol SettitleteXtattributes:highlightedattributes forstate:uicontrolstatehighlighted]; Set the segment control to click on the corresponding event [Segmentedcontrol addtarget:self Action: @selector (dosomethinginsegment:) forControlEvents:        Uicontroleventvaluechanged]; [Self.view Addsubview:segmentedcontrol];}

Each function gaze has a clear description of the narrative, one thing to be specific:

At iOS7 once, Segmentedcontrol has a Segmentedcontrolstyle attribute, which is usually set, for example, as follows:

/*     typedef enum {     Uisegmentedcontrolstyleplain,     uisegmentedcontrolstylebordered,     Uisegmentedcontrolstylebar,     uisegmentedcontrolstylebezeled,     } uisegmentedcontrolstyle; Segmentedcontrol.segmentedcontrolstyle = Uisegmentedcontrolstylebar;


But after the iOS7, these styles are not valid for the sake of flat style.

Let's write a button's event response function and set a different background image, such as the following:

-(void) Dosomethinginsegment: (Uisegmentedcontrol *) seg{        Nsinteger Index = seg.selectedsegmentindex;        Switch (Index)    {case        0:            self.view.backgroundColor = [Uicolor colorwithpatternimage:[uiimage imagenamed: Ksrcname (@ "Bg_apple_small.png")];            break;        Case 1:            self.view.backgroundColor = [Uicolor colorwithpatternimage:[uiimage imagenamed:ksrcname (@ "Bg_orange_ Small.png ")];            break;        Case 2:            self.view.backgroundColor = [Uicolor colorwithpatternimage:[uiimage imagenamed:ksrcname (@ "Bg_banana_ Small.png ")];            break;        Default: Break            ;    }}

The code is relatively simple. The key is to understand the application scenarios of segmented control, flexible use. In addition to the first picture of the game program, I here to give a sample, very often the segmented control nested in the navigation bar to use, in order to reduce the number of levels between Navigationview, to give users a better experience , Just like this:


Source code Download:

Https://github.com/pony-maggie/SegmentedControl

Or

http://download.csdn.net/detail/pony_maggie/7403175

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.