Summary of UISegmentedControl segmentation components in iOS App development

  • 2020-06-07 05:22:09
  • OfStack

The UISegmentedControl segmentation control replaces the radio buttons on the desktop OS. But its options are limited because your IOS device has a limited screen. It works 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 location or size specified? Yes, I did find initWithItems and not initWithFrame in his class declaration, so he doesn't need to specify it, but I see another method that sets the width of Item:

mySegmentedControl setWidth:100 forSegmentAtIndex:0];// Set up the Item The width of the  

2. The attribute

mySegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;// style  

Depending on the occasion, there are 3 styles to choose from, as follows:

typedef enum { 
    UISegmentedControlStylePlain,     // large plain White button with grey edge for preference setting unit  
    UISegmentedControlStyleBordered,  // large bordered White button with black edge for table cells  
    UISegmentedControlStyleBar,       // small button/nav bar style. tintable Small buttons for navigation  
    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 render color for the entire control:

UIColor *myTint = [[ UIColor alloc]initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0]; 
mySegmentedControl.tintColor = myTint; 

3. Add and delete fragments
Each fragment of a segmented control is a button containing a label or image. You need to create 1 fragment for each control in your control. As long as the screen has room, there can be many segments, but the user can only select one segment at a time.

[mySegmentedControl insertSegmentWithTitle:@"First" atIndex:0 animated:YES]; 
[mySegmentedControl insertSegmentWithTitle:@"Second" atIndex:2 animated:YES];

each
Buttons are assigned an index, sorted by the cable, and identified.
You can also add a fragment with an image, using inserSegmentWithImage

[mySegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"pic"]  atIndex:3 animated:YES];

Delete the clip

[mySegmentedControl removeSegmentAtIndex:0 animated:YES];// delete 1 A fragment  
[mySegmentedControl removeAllSegments];// Delete all segments

4. Segment title

[mySegmentedControl setTitle:@"ZERO" forSegmentAtIndex:0];// Set the title  
NSString* myTitle = [mySegmentedControl titleForSegmentAtIndex:1];// Read the title  

Image 5.
Images can also be set for each segment:

[mySegmentedControl setImage:[UIImage imageNamed:@"pic"] forSegmentAtIndex:1];// Set up the  
UIImage* myImage = [mySegmentedControl imageForSegmentAtIndex:2];// read  

Note: Images will not automatically be resized, as the size of the image will naturally be displayed, so you need to tell the artist who made the image to be exact in size.

6. Select segments
The default behavior of a segmented control is that once a button is selected, it remains 1 until another button is selected. You can change this default behavior to automatically release the button after it is pressed. Set the momentary property of the control to YES:


mySegmentedControl.momentary = YES; 

Note: Touching fragments does not update selectedSegmentedIndex when this feature is enabled, so the currently selected fragment cannot be obtained through this property.
Initialize the default fragment
By default, no fragment is selected unless you specify it. To set the selectedSegmentedIndex property:

mySegmentedControl.selectedSegmentedIndex = 0; 

7. Display controls

[parentView addSubview:mySegmentedControl];// Add to the superview  

or

self.navigationItem.titleView = mySegmentedControl;// Add to navigation bar  

Read the control
The selectedSegmentedIndex attribute reads the value of the currently selected fragment, which is the index number of the selected fragment.

int x = mySegmentedControl. selectedSegmentedIndex; 

9. Inform
To receive notification of fragment selection, add 1 action to the UIControlEventValueChanged event using the addTarget method of the UIControl class:

[mySegmentedControl addTarget:self action:@selector(selected:) forControlEvents:UIControlEventValueChanged]; 

Once 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; 
    } 


10. Set rounded corners and set the selected color to null

UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@" Set up the ",@" Know the ", nil]]; seg.frame = CGRectMake(10,0, CGRectGetWidth(self.view.frame) - 20, 35); seg.center = isbluetoothOffAlerView.center; seg.layer.borderColor = [UIColor whiteColor].CGColor; seg.layer.borderWidth = 2; seg.tintColor = [UIColor whiteColor]; seg.backgroundColor = [UIColor clearColor]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont systemFontOfSize:24],NSFontAttributeName,nil]; [seg setTitleTextAttributes:dic forState:UIControlStateNormal]; seg.layer.cornerRadius = 15; seg.layer.masksToBounds = YES;


Related articles: