UISegmentedControl for basic iOS controls

Source: Internet
Author: User

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;

Related Article

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.