Some common attributes of segmentedControl in IOS, segmentedcontrol

Source: Internet
Author: User

Some common attributes of segmentedControl in IOS, segmentedcontrol

# Pragma mark-create different slots

// Initialization method: the input array can be a string or an image array of the UIImage object

UISegmentedControl * mysegmented = [[UISegmentedControl alloc] initWithItems: @ [@ "Left", @ "Middle", @ "right"];

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

[Self. view addSubview: mysegmented];

// Method 2

UISegmentedControl * mysegmented2 = [[UISegmentedControl alloc] initWithFrame: CGRectMake (100,200,300, 40)];

// If the value of animated is "NO", it indicates that it is not modified in real time. After the click event ends, the system will respond.

[Mysegmented2 insertSegmentWithTitle: @ "chat" atIndex: 1 animated: YES];

[Mysegmented2 insertSegmentWithTitle: @ "" atIndex: 0 animated: YES];

// [Self. view addSubview: mysegmented2];

// Create an image array and use the image as an element of the field splitter.

NSArray * imageArray = @ [[UIImage imageNamed: @ "prev"], [UIImage imageNamed: @ "next"];

UISegmentedControl * yoursegmented = [[UISegmentedControl alloc] initWithItems: imageArray];

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

[Self. view addSubview: yoursegmented];


# Pragma mark-resetting the label title and obtaining the label title

[Mysegmented setTitle: @ "My modified tags" forSegmentAtIndex: 1];

NSString * titleString = [mysegmented titleForSegmentAtIndex: 1];

NSLog (@ "% @", titleString );

// You can also reset the Image Tag and obtain the Tagged Image.

//-(Void) setImage :( UIImage *) image forSegmentAtIndex :( NSUInteger) segment;

//-(UIImage *) imageForSegmentAtIndex :( NSUInteger) segment;


/* The following describes the usage and resolution of some segmentedControl attributes in ios development. You can only use your mobile phone to view the attributes that cannot be run by yourself. The following content contains images. */


# Pragma mark-set the label color (blue by default) and Image

// Background color

Mysegmented. backgroundColor = [UIColor greenColor];

// Text color, border color, and selected label color

Mysegmented. tintColor = [UIColor redColor];

// Set the background image of the tag and obtain the background image of the field splitter.

// [Mysegmented setBackgroundImage: [UIImage imageNamed: @ "prev"] forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];

// (The background image of the entire field splitter is set), UIBarMetrics is an enumeration, And the default background image is full of the entire field splitter by default.




# Pragma mark-set the split line image of the splitter (you can also get the image)

//-(Void) setDividerImage :( UIImage *) dividerImage forLeftSegmentState :( UIControlState) leftState rightSegmentState :( UIControlState) rightState barMetrics :( UIBarMetrics) barMetrics



# Pragma mark-selected status (if no subscript is specified, the subscript increases from 0 according to the items order created)

Mysegmented. selectedSegmentIndex = 0;

Yoursegmented. selectedSegmentIndex = 1;





# Pragma mark-numberOfSegments, get the number of tags, read-only

NSInteger I = mysegmented. numberOfSegments;

NSLog (@ "% ld tags in total", I );



# Pragma mark-set whether the label width is adaptive to the content (if it is set to NO, it indicates that the width of all labels is the same as the maximum width)

Mysegmented. apportionsSegmentWidthsByContent = NO;

# Pragma mark-insert a text label or image at the index position

[Mysegmented insertSegmentWithTitle: @ "My inserted tags" atIndex: 2 animated: YES];

[Yoursegmented insertSegmentWithImage: [UIImage imageNamed: @ "next"] atIndex: 0 animated: YES];



# Pragma mark-delete tags Based on indexes or delete all tags

[Mysegmented removeSegmentAtIndex: 1 animated: YES];

// [Mysegmented removeSegmentAtIndex: 1 animated: NO];

// Delete All tags [mysegmented removeAllSegments];





# Pragma mark-set the tag width and obtain the tag width based on the index.

// If setWidth is set to 0.0, the default value is adaptive width.

[Yoursegmented setWidth: 50.0 forSegmentAtIndex: 0];

// Obtain the tag width

//-(CGFloat) widthForSegmentAtIndex :( NSUInteger) segment;





# Pragma mark-set the offset of the TAG content and obtain the offset

[Mysegmented setContentOffset: CGSizeMake (20.0, 0) forSegmentAtIndex: 0];

// Get the offset

//-(CGSize) contentOffsetForSegmentAtIndex :( NSUInteger) segment;



# Pragma mark-set whether the tag is valid (valid by default) and whether the tag is valid Based on the index.

//-(Void) setEnabled :( BOOL) enabled forSegmentAtIndex :( NSUInteger) segment;

//-(BOOL) isEnabledForSegmentAtIndex :( NSUInteger) segment;


# Pragma mark-Click Event (parameters can be included)

[Mysegmented addTarget: self action: @ selector (segmentedAction) forControlEvents: UIControlEventValueChanged];

// Create another View Controller for demonstration.



// Click an event

-(Void) segmentedAction {

FirstViewController * firstVC = [[FirstViewController alloc] init];

[Self. view addSubview: firstVC. view];

// Remove [_ firstVC. view removeFromSuperview] from the parent view;






The above are some of the basic attributes of the field splitter control I have summarized. I can cut the image and copy and paste the above code to my project for running. You can view and modify the result on your own. If you have any objection or want to discuss it with me, you can add my QQ 78928861. Let's discuss it and study it together.


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.