Introduction to Using iOS Uisegmentedcontrol

Source: Internet
Author: User

Initialize Uisegmentedcontrol

Nsarray *arr = [[Nsarray alloc]initwithobjects:@ "Pat", @ "long press", @ "sweep", @ "rotate", @ "pinch", @ "drag", nil];

First, create an array to set the caption

Uisegmentedcontrol *segment = [[Uisegmentedcontrol Alloc]initwithitems:arr];

When [segment Setapportionssegmentwidthsbycontent:yes] is not set, each width is divided equally by the width of the segment

Segment.frame = CGRectMake (0, 400, 320, 40);

Set frame

Managing the content of segment

[Segment settitle:@ "stool" forsegmentatindex:3];

Set subscript 3 for the title of the segment subscript with 0 start all subscripts in iOS start with 0

[Segment Setimage:[uiimage imagenamed:@ "3"] forsegmentatindex:4];

Set the picture of the segment labeled 4

Management Segments

[Segment Insertsegmentwithimage:[uiimage imagenamed:@ "3"] atindex:2 Animated:yes]; Set up a picture

[Segment insertsegmentwithtitle:@ "DDD" atindex:0 animated:yes];//set title

[Segment numberofsegments];//Get the number of segment

[Segment removeallsegments];//Remove all segment

[Segment Removesegmentatindex:2 animated:yes];//Remove the subscript 2 segment

Segment.selectedsegmentindex = 0;//Check the first few segment are typically used for initialization when selected

Manage the behavior and appearance of segment

Segment.momentary = NO;

The default is no when set to Yes, when selected, does not show the selected state after a few moments (the leftmost appearance), not the right button of the state is selected


Segment.segmentedcontrolstyle = Uisegmentedcontrolstylebar;

typedef enum {
Uisegmentedcontrolstyleplain,
Uisegmentedcontrolstylebordered,
Uisegmentedcontrolstylebar,
Uisegmentedcontrolstylebezeled,
} Uisegmentedcontrolstyle;

Sets the style when bordered and bar tintcolor are effective when tintcolor is red

Bar style

Bordered

Now use will report a warning because (Ns_deprecated_ios (2_0, 7_0, "the Segmentedcontrolstyle property no longer have any effect") __tvos_ prohibited;)

[Segment Setenabled:no]; Sets whether segment is available This method is the Uicontrol method of its parent class

[Segment Setenabled:no forsegmentatindex:2];//set subscript 2 segment not available

[Segment setwidth:100 Forsegmentatindex:2]; The following table is the width of the segment of 2

[Segment Setcontentoffset:cgsizemake (Ten) forsegmentatindex:2];//set content offset

Segment.apportionssegmentwidthsbycontent = YES; Whether to change the width of the segment according to the contents of segment

customizing skins

[Segment Settintcolor:[uicolor Redcolor]; Set the color of the segments

[Segment Setbackgroundimage:[uiimage imagenamed:@ "] forstate:uicontrolstatenormal Barmetrics:uibarmetricsdefault] ;

typedef enum {
Uibarmetricsdefault,//Vertical screen
Uibarmetricslandscapephone, Horizontal screen
} uibarmetrics;

Set the background picture to segments in a certain state

[Segment Settitletextattributes:dic Forstate:uicontrolstatenormal];


NSString *const Uitextattributefont; Value:uifont
NSString *const Uitextattributetextcolor; Value:uicolor
NSString *const Uitextattributetextshadowcolor; Value:uicolor
NSString *const Uitextattributetextshadowoffset; Value:nsvalue Wrapping a Uioffset

Nsdictionary *dic = [Nsdictionary dictionarywithobjectsandkeys:[uicolor redcolor],uitextattributetextcolor,[uifont fontwithname:@ "Snellroundhand-bold" Size:24],uitextattributefont, nil];

Set the title's color font and size shadow and shadow color

[Segment Addtarget:self Action: @selector (change:) forcontrolevents:uicontroleventvaluechanged];

When a different segment is selected, the change is performed: method

The Uisegmentedcontrol segmented control replaces the radio button on the Desktop OS. However, it has a very limited number of options because your iOS device has a limited screen. It is appropriate when we need to use a radio button with very few options.

First, create

Uisegmentedcontrol*mysegmentedcontrol = [[Uisegmentedcontrolalloc]initwithitems:nil];

Isn't it strange to not 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 it, but I saw another way to set the width of the item:

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

Second, the attribute

    1. Mysegmentedcontrol.segmentedcontrolstyle = uisegmentedcontrolstylebar;//Style

Depending on the occasion of use, there are three styles to choose from, as follows:

    1. typedef ENUM {
    2. Uisegmentedcontrolstyleplain,//large plain white button with grey edges for preference unit
    3. uisegmentedcontrolstylebordered,//large bordered white button on black edge for table cells
    4. Uisegmentedcontrolstylebar,//small Button/nav bar style. Tintable small button, suitable for navigation bar
    5. uisegmentedcontrolstylebezeled,//large bezeled style. Tintable
    6. } Uisegmentedcontrolstyle;

If you are using the Uisegmentedcontrolstylebar style, you can also use the Tintcolor property of the space to set the rendering color for the entire control:

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

Third, add, delete fragments

The fragment of each segmented control is 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 is placed, there can be many fragments, but the user can only select one clip at a time.

    1. [Mysegmentedcontrol insertsegmentwithtitle:@ "First" atindex:0 Animated:yes];
    2. [Mysegmentedcontrol insertsegmentwithtitle:@ "Second" Atindex:2 Animated:yes];

Each
Buttons are assigned an index, which is used to sort and identify the cable.
You can also add a fragment containing an image, using the Insersegmentwithimage

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

Delete a fragment

    1. [Mysegmentedcontrol removesegmentatindex:0 animated:yes];//Delete a fragment
    2. [Mysegmentedcontrol removeallsegments];//Delete all clips

Iv. title of the Fragment

    1. [Mysegmentedcontrol settitle:@ "ZERO" forsegmentatindex:0];//set title
    2. nsstring* myTitle = [Mysegmentedcontrol titleforsegmentatindex:1];//Read Header

Five, image

You can also set the image for each segment:

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

Note: The image will not be automatically resized, the size of the picture will be native to show how big, so you want to inform the artwork size to be accurate.
Six, select the segment

The default behavior of a segmented control is that it remains until the button is selected, until another button is selected. You can change this default behavior by changing the button to automatically release it soon after it is pressed. Set the momentary property of the control to Yes:

    1. Mysegmentedcontrol.momentary = YES;

Note: When this feature is turned on, the touch fragment does not update the Selectedsegmentedindex, so it is not possible to get the currently selected clip from this property.

Initialize default fragment

By default, no fragment is selected unless you specify it. To set the Selectedsegmentedindex property:

    1. Mysegmentedcontrol.selectedsegmentedindex = 0;

Vii. Display Controls

    1. [Parentview addsubview:mysegmentedcontrol];//added to Parent view
    2. Or
    3. Self.navigationItem.titleView = mysegmentedcontrol;//added to the 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.

    1. int x = Mysegmentedcontrol. Selectedsegmentedindex;

IX. Notice

To receive notification of clip selection, you can add an action to the Uicontroleventvaluechanged event using the AddTarget method of the Uicontrol class:

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

As soon as a fragment is selected, your action method is called:

    1. -(void) selected: (ID) sender{
    2. uisegmentedcontrol* control = (uisegmentedcontrol*) sender;
    3. Switch (Control.selectedsegmentindex) {
    4. Case 0:
    5. //
    6. break;
    7. Case 1:
    8. //
    9. break;
    10. Case 2:
    11. //
    12. break;
    13. default:
    14. break;
    15. }
    16. }

Introduction to Using iOS Uisegmentedcontrol

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.