Some common properties of iOS Segmentedcontrol (segment control)

Source: Internet
Author: User

#pragma mark-Create a different segmentation device

Initialization method: The incoming array can be either a string or a picture array of the UIImage object

Uisegmentedcontrol *mysegmented = [[Uisegmentedcontrol alloc] initwithitems:@[@ "left", @ "I am Middle", @ "right"];

Mysegmented.frame = CGRectMake (kScreenWidth-100, 40);

[Self.view addsubview:mysegmented];

Second method of Creation

Uisegmentedcontrol *mysegmented2 = [[Uisegmentedcontrol alloc] Initwithframe:cgrectmake (100, 200, 300, 40)];

Animated is no instant modification, and then responds when the Click event ends

[Mysegmented2 insertsegmentwithtitle:@ "chat" atindex:1 Animated:yes];

[Mysegmented2 insertsegmentwithtitle:@ "call" atindex:0 Animated:yes];

[Self.view Addsubview:mysegmented2];

Create an array of images to use as elements of the segment

Nsarray *imagearray = @[[uiimage imagenamed: @ "prev"],[uiimage imagenamed: @ "Next"];

Uisegmentedcontrol *yoursegmented = [[Uisegmentedcontrol alloc] initwithitems:imagearray];

Yoursegmented.frame = CGRectMake (kScreenWidth-200, 65);

[Self.view addsubview:yoursegmented];

#pragma mark-resets the label title and gets the label title

[mysegmented settitle: @ "I am the Modified label" forsegmentatindex:1];

NSString *titlestring = [mysegmented titleforsegmentatindex:1];

NSLog (@ "%@", titlestring);

You can also reset the picture label and get the label picture

-(void) SetImage: (UIImage *) image forsegmentatindex: (Nsuinteger) segment;

-(UIImage *) Imageforsegmentatindex: (Nsuinteger) segment;

/* Following is the use of some of the properties of the Segmentedcontrol control in iOS development, can only be used to view the mobile phone can not run their own, there is no relationship, the following is a picture. */

#pragma mark-Sets the color of the label (blue by default) and the picture

Background color

Mysegmented.backgroundcolor = [Uicolor Greencolor];

The color of the text, the color of the border, and the color of the selected label

Mysegmented.tintcolor = [Uicolor Redcolor];

Set the background picture of the label, and also get the background picture of the segmented device

[mysegmented setbackgroundimage:[uiimage imagenamed:@ "prev"] forstate:uicontrolstatenormal barMetrics: Uibarmetricsdefault];

(Set the background image of the entire segment), Uibarmetrics is an enumeration, default background picture fills the entire segment

#pragma mark-Set a picture of the Splitter's split line (same, you can get the picture)

-(void) Setdividerimage: (UIImage *) dividerimage forleftsegmentstate: (uicontrolstate) Leftstate rightsegmentstate :(uicontrolstate) rightstate barmetrics: (uibarmetrics) Barmetrics

#pragma mark-Check status (if you do not specify subscript, the subscript starts from 0 based on the order of items created)

Mysegmented.selectedsegmentindex = 0;

Yoursegmented.selectedsegmentindex = 1;

#pragma mark-numberofsegments, gets the number of tags, read-only

Nsinteger i = mysegmented.numberofsegments;

NSLog (@ "There are altogether%ld tags", i);

#pragma mark-Sets whether the label width is adaptive to the content (set to No to indicate the width of all label widths consistent to the maximum)

Mysegmented.apportionssegmentwidthsbycontent = NO;

#pragma mark-Insert text label or picture at index position

[mysegmented insertsegmentwithtitle: @ "I am inserting the label" Atindex:2 Animated:yes];

[yoursegmented insertsegmentwithimage: [UIImage imagenamed: @ "Next"] atindex:0 Animated:yes];

#pragma mark-delete labels or remove all labels by index

[mysegmented removesegmentatindex:1 Animated:yes];

[mysegmented removesegmentatindex:1 Animated:no];

Delete all tags [mysegmented removeallsegments];

#pragma mark-Set the corresponding label width and get the label width according to the index

If SetWidth is set to 0.0, the default is adaptive width

[yoursegmented setwidth:50.0 forsegmentatindex:0];

Get label width

-(CGFloat) Widthforsegmentatindex: (Nsuinteger) segment;

#pragma mark-Sets the offset of the label content and gets the offset amount

[Mysegmented setcontentoffset:cgsizemake (20.0, 0) forsegmentatindex:0];

Get offset

-(Cgsize) Contentoffsetforsegmentatindex: (Nsuinteger) segment;

#pragma mark-Sets whether the label is valid according to the index (by default) and whether the fetch is valid

-(void) setenabled: (BOOL) enabled Forsegmentatindex: (Nsuinteger) segment;

-(BOOL) Isenabledforsegmentatindex: (Nsuinteger) segment;

#pragma mark-click events (can take parameters)

[Mysegmented addtarget:self Action: @selector (Segmentedaction) forcontrolevents:uicontroleventvaluechanged];

Here you need to create another view controller for the demo

}

Click events

-(void) segmentedaction{

Firstviewcontroller *FIRSTVC = [[Firstviewcontroller alloc] init];

[Self.view AddSubview:firstVC.view];

Remove [_firstvc.view Removefromsuperview] from the parent view;

}

/*

The above is my own summary of the segmentation of some basic properties of the control, cut off the picture, the above code can be copied directly to the project to run, you can view and modify the results. Objection to the above or want to discuss with me, you can add my QQ 78928861, we discuss together, study together.

*/

Some common properties of iOS Segmentedcontrol (segment control)

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.