UISegmentedControl for basic iOS controls
* ** The UISegmentedControl control is a commonly used control in iOS. As an iOS Mobile phone device screen, multiple UIButton is put at once, which obviously does not feel beautiful, as a manager who does not compromise and pursues excellence, he is not willing to design the control to meet the usual needs. ***
NSArray * segmentedArray = [[NSArray alloc] initWithObjects: @ "1", @ "2", @ "3", @ "4", nil];
The UISegmentedControl is similar to the previous control:
SegmentedControl = [[UISegmentedControl alloc] initWithItems: segmentedArray];
SegemntedControl. frame = CGRectMake (80,180,250, 60); control initialization and location
The UISegmentedControl control is a custom control:
SegmentedControl. selectedSegmentIndex = 1; // the index of the selected item by default (the count starts from 0)
SegmentedControl. tintColor = [UIColor redColor]; // you can specify the background color.
SegmentedControl. momentary = YES; // you can specify whether to restore the status after clicking
[Self. segmentedControl setTitle: @ "YES" forSegmentAtIndex: 1]; // you can specify the index title.
[Self. view addSubview: segmentedControl];
// Set the style of the control. However, I tried it and got a warning. I don't know if it was canceled or modified in ios8. to be determined!
SegmentedControl. segmentedControlStyle = UISegmentedControlStyleBar;
Several styles:
Typedef enum
{
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar,
UISegmentedControlStyleBezeled
} Style;
Each section is actually a small button UIButton. You can set tags, backgrounds, and listener events for this.
Listening events are the same as other controls:
[SegmentedControl addTarget: self action: @ selector (segmentedAction :) forControlEvents: UIControlEventValueChanged];
-(Void) segmentedAction :( UISegmentedControl *) Seg
{
NSInteger index = Seg. selectedSegmentIndex;
Switch (index ){
Case 0:
NSLog (@ "% ld", (long) index );
Break;
case 1: NSLog(@"%ld",(long)index); break; case 2: NSLog(@"%ld",(long)index); break; case 3: NSLog(@"%ld",(long)index); break;}
}
[Self. segmentedControl setImage: [UIImage imageNamed: @ ""] forSegmentAtIndex: 3]; // sets the image of the specified index
Add the short button at the specified position:
[SegmentedControl1 insertSegmentWithTitle: @ "Add" atIndex: 0 animated: YES];
Press whether to release automatically:
SegmentedControl1.momentary = YES;
Multi-touch support:
SegmentedControl. multipleTouchEnabled = NO;
It is shown that the control is generally used in two development scenarios:
1. display on common view
[Self. view addSubview: segmentedControl];
2. displayed on the navigation bar,
Self. navigationItem. titleView = segmentedControl;