Summary of Uisegmentedcontrol segmented component usage in IOS app development _ios

Source: Internet
Author: User

The Uisegmentedcontrol fragment control replaces radio buttons on the Desktop OS. However, the number of options is very limited because your iOS device screen is limited. It fits when we need a radio button with very few options.
First, create

Copy Code code as follows:

uisegmentedcontrol* Mysegmentedcontrol = [[Uisegmentedcontrol Alloc]initwithitems:nil];


Is it strange not to specify the location and size? Yes, I did find initwithitems in his class statement and didn't find initWithFrame, so he didn't have to specify, but I saw another way to set the width of the item:
Copy Code code as follows:

Mysegmentedcontrol setwidth:100 forsegmentatindex:0];//Set the width of the item

second, the property
Copy Code code as follows:

Mysegmentedcontrol.segmentedcontrolstyle = uisegmentedcontrolstylebar;//Style

Depending on the use of the occasion, there are three styles of choice, as follows:
Copy Code code as follows:

typedef enum {
Uisegmentedcontrolstyleplain,//large plain with gray-edged white button, suitable for preference unit
uisegmentedcontrolstylebordered,//large bordered black-edged white button, for table cells
Uisegmentedcontrolstylebar,//small Button/nav bar style. Tintable small button, suitable for navigation bar
uisegmentedcontrolstylebezeled,//large bezeled style. Tintable
} Uisegmentedcontrolstyle;

If you are using a uisegmentedcontrolstylebar style, you can also use the Space Tintcolor property to set the rendering color for the entire control:
Copy Code code as follows:

Uicolor *mytint = [[Uicolor alloc]initwithred:0.66 green:1.0 blue:0.77 alpha:1.0];
Mysegmentedcontrol.tintcolor = Mytint;

Add, delete fragments
Fragments of each segmented control are a button that contains a label or picture. You need to create a fragment for each control in your control. As long as the screen can be placed, there are many fragments, but the user can only select one fragment at a time.
Copy Code code as follows:

[Mysegmentedcontrol insertsegmentwithtitle:@ "a" atindex:0 animated:yes];
[Mysegmentedcontrol insertsegmentwithtitle:@ "Second" Atindex:2 Animated:yes];

Each
Buttons are assigned an index, sorted with this cable, and labeled.
You can also add a fragment containing the image, using the Insersegmentwithimage
Copy Code code as follows:

[Mysegmentedcontrol insertsegmentwithimage:[uiimage imagenamed:@ "pic"] atindex:3 Animated:yes];

Delete Fragment
Copy Code code as follows:

[Mysegmentedcontrol removesegmentatindex:0 animated:yes];//Deletes a fragment
[Mysegmentedcontrol removeallsegments];//Delete all fragments

Iv. Fragment Title
Copy Code code as follows:

[Mysegmentedcontrol settitle:@ ' ZERO ' forsegmentatindex:0];//set the title
nsstring* myTitle = [Mysegmentedcontrol titleforsegmentatindex:1];//Read headers

Five, image
You can also set an image for each fragment:
Copy Code code as follows:

[Mysegmentedcontrol setimage:[uiimage imagenamed:@ "pic"] forsegmentatindex:1];//settings
uiimage* myimage = [Mysegmentedcontrol imageforsegmentatindex:2];//read

Note: The image will not automatically resize, how large the picture will be native to show how large, so you want to inform the art of drawing to the size of precision.

Vi. selected Sections
The default behavior of a segmented control is to persist once the button is selected until another button is selected. You can change this default behavior and turn it into a button that is automatically released soon after it is pressed. Set the control's momentary property to Yes:

Copy Code code as follows:

Mysegmentedcontrol.momentary = YES;

Note: The touch fragment does not update Selectedsegmentedindex when this feature is turned on, so the currently selected fragment cannot be obtained through this property.
Initialize default fragment
By default, no fragment is selected unless you specify it. To set the Selectedsegmentedindex property:
Copy Code code as follows:

Mysegmentedcontrol.selectedsegmentedindex = 0;

Seven, display control
Copy Code code as follows:

[Parentview addsubview:mysegmentedcontrol];//Add to Parent view

Or
Copy Code code as follows:

Self.navigationItem.titleView = mysegmentedcontrol;//Add to navigation bar

Eight, read the control
The Selectedsegmentedindex property allows you to read the value of the currently selected segment, which is the index number of the selected fragment.
Copy Code code as follows:

int x = Mysegmentedcontrol. Selectedsegmentedindex;

ix. Notice
To receive notification of a fragment selection, you can add an action for the Uicontroleventvaluechanged event using the Addtarget method of the Uicontrol class:
Copy Code code as follows:

[Mysegmentedcontrol addtarget:self Action: @selector (selected:) forcontrolevents:uicontroleventvaluechanged];

Once you have selected a fragment, your action method is invoked:
Copy Code code as follows:

-(void) selected: (ID) sender{
uisegmentedcontrol* control = (uisegmentedcontrol*) sender;
Switch (control.selectedsegmentindex) {
Case 0:
//
Break
Case 1:
//
Break
Case 2:
//
Break

Default
Break
}
}

10. Set rounded corners and set the selected color to null
Copy Code code as follows:

Uisegmentedcontrol *seg = [[Uisegmentedcontrol alloc]initwithitems:[nsarray arraywithobjects:@ "Settings" @ "Know", Nil]];

Seg.frame = CGRectMake (10,0, Cgrectgetwidth (self.view.frame)-20, 35);

Seg.center = Isbluetoothoffalerview.center;

Seg.layer.borderColor = [Uicolor Whitecolor]. Cgcolor;

Seg.layer.borderWidth = 2;

Seg.tintcolor = [Uicolor Whitecolor];

Seg.backgroundcolor = [Uicolor Clearcolor];

Nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:[uicolor whitecolor], NSForegroundColorAttributeName, [Uifont Systemfontofsize:24],nsfontattributename,nil];

[Seg Settitletextattributes:dic forstate:uicontrolstatenormal];

Seg.layer.cornerRadius = 15;

Seg.layer.masksToBounds = YES;

Related Article

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.