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:
- Mysegmentedcontrol setwidth:100 forsegmentatindex:0];//Set the width of the item
Second, the attribute
- Mysegmentedcontrol.segmentedcontrolstyle = uisegmentedcontrolstylebar;//Style
Depending on the occasion of use, there are three styles to choose from, as follows:
- typedef ENUM {
- Uisegmentedcontrolstyleplain,//large plain white button with grey edges for preference unit
- uisegmentedcontrolstylebordered,//large bordered white button on black edge 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 the Uisegmentedcontrolstylebar style, you can also use the Tintcolor property of the space to set the rendering color for the entire control:
- Uicolor *mytint = [[Uicolor alloc]initwithred:0.66 green:1.0 blue:0.77 alpha:1.0];
- 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.
- [Mysegmentedcontrol insertsegmentwithtitle:@ "First" atindex:0 Animated:yes];
- [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
- [Mysegmentedcontrol insertsegmentwithimage:[uiimage imagenamed:@ "pic"] atindex:3 Animated:yes];
Delete a fragment
- [Mysegmentedcontrol removesegmentatindex:0 animated:yes];//Delete a fragment
- [Mysegmentedcontrol removeallsegments];//Delete all clips
Iv. title of the Fragment
- [Mysegmentedcontrol settitle:@ "ZERO" forsegmentatindex:0];//set title
- nsstring* myTitle = [Mysegmentedcontrol titleforsegmentatindex:1];//Read Header
Five, image
You can also set the image for each segment:
- [Mysegmentedcontrol setimage:[uiimage imagenamed:@ "pic"] forsegmentatindex:1];//settings
- 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:
- 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:
- Mysegmentedcontrol.selectedsegmentedindex = 0;
Vii. Display Controls
- [Parentview addsubview:mysegmentedcontrol];//added to Parent view
- Or
- 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.
- 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:
- [Mysegmentedcontrol addtarget:self Action: @selector (selected:) forcontrolevents:uicontroleventvaluechanged];
As soon as a fragment is selected, your action method is called:
- -(void) selected: (ID) sender{
- uisegmentedcontrol* control = (uisegmentedcontrol*) sender;
- Switch (Control.selectedsegmentindex) {
- Case 0:
- //
- break;
- Case 1:
- //
- break;
- Case 2:
- //
- break;
- default:
- break;
- }
- }
Introduction to Using iOS Uisegmentedcontrol