The UISegmentedControl segmentation control replaces the radio buttons on desktop OS. However, its options are very limited because your IOS device has limited screens. It fits well when we need to use radio buttons with very few options.
1. Create
UISegmentedControl* mySegmentedControl = [[UISegmentedControl alloc]initWithItems:nil];
Isn't it strange that there is no specified position and size? Yep, I did find only initWithItems in his class declaration and not initWithFrame, so he doesn't need to specify it, but I saw another method, which can set the width of the Item:
mySegmentedControl setWidth:100 forSegmentAtIndex:0];//Set the width of the Item
2. Attributes
= UISegmentedControlStyleBar;//Style
Depending on the occasion of use, there are three styles to choose from, as follows:
typedef enum {
UISegmentedControlStylePlain, // large plain Big white button with gray edges, suitable for preference settings unit
UISegmentedControlStyleBordered, // large bordered The big white button with black edges is suitable 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];
= myTint;
3. Add and delete clips
Each segment control's fragment is a button that contains a label or image. You need to create a fragment for each control in your control. As long as the screen can be placed, there can be many clips, but the user can only choose one clip at the same time.
[mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:YES];
[mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:2 animated:YES];
Each
Buttons are all given an index, sorted and identified by this cable.
You can also add a clip containing the image and use insertSegmentWithImage
[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"] atIndex:3 animated:YES];
Delete a fragment
[mySegmentedControl removeSegmentAtIndex:0 animated:YES];//Delete a fragment
[mySegmentedControl removeAllSegments];//Delete all fragments
4. Fragment title
[mySegmentedControl setTitle:@"ZERO" forSegmentAtIndex:0];//Set the title
NSString* myTitle = [mySegmentedControl titleForSegmentAtIndex:1];//Read the title
V. Image
Images can also be set 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, and the size of the image will be displayed natively, so you need to notify the artist who makes the picture to be accurate.
6. Select the segment
The default behavior of segmented controls is to keep it once the button is selected until another button is selected. You can change this default behavior and automatically release it quickly after pressing the button. Set the momentary property of the control to YES:
= YES;
Note: After turning on this function, touching the clip will not update the selectedSegmentedIndex, so the currently selected clip cannot be obtained through this property.
Initialize the default fragment
By default, no fragments are selected unless you specify them. To set the selectedSegmentedIndex property:
= 0;
7. Display controls
[parentView addSubview:mySegmentedControl];//Add to parent view
or
= mySegmentedControl;//Add to navigation bar
8. Read the control
Through the selectedSegmentedIndex property, the value of the currently selected segment can be read, which is the index number of the selected segment.
int x = mySegmentedControl. selectedSegmentedIndex;
9. Notification
To receive notifications for fragment selection, you can use the addTarget method of the UIControl class to add an action to the UIControlEventValueChanged event:
[mySegmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged];
As long as a fragment is selected, your action method will be called:
-(void)selected:(id)sender{
UISegmentedControl* control = (UISegmentedControl*)sender;
switch () {
case 0:
//
break;
case 1:
//
break;
case 2:
//
break;
default:
break;
}
}
10. Set rounded corners and set the selected color to empty
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Set",@"Import", nil]];
= CGRectMake(10,0, CGRectGetWidth() - 20, 35);
= ;
= [UIColor whiteColor].CGColor;
= 2;
= [UIColor whiteColor];
= [UIColor clearColor];
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:24],NSFontAttributeName,nil];
[seg setTitleTextAttributes:dic forState:UIControlStateNormal];
= 15;
= YES;