IOS UI界面 UISegmentedControl的基本用法

IOS UI界面 UISegmentedControl的基本用法_第1张图片
WX20181105-151048@2x.png
//初始化UISegmentedControl
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"1080P", @"720P", @"540P", @"480P"]];

// 设置默认选择项索引
segmentedControl.selectedSegmentIndex = 2;

segmentedControl.tintColor = [UIColor whiteColor];

// 设置在点击后是否恢复原样
segmentedControl.momentary = YES;

//设置指定索引的题目
[segmentedControl setTitle:@"two" forSegmentAtIndex:1];

//设置指定索引的图片
[segmentedControl setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];

// 在指定索引插入一个选项并设置图片
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];

// 在指定索引插入一个选项并设置题目
[segmentedControl insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];

// 移除指定索引的选项
[segmentedControl removeSegmentAtIndex:0 animated:NO];

// 设置指定索引选项的宽度
[segmentedControl setWidth:70.0 forSegmentAtIndex:2];

// 设置选项中图片等的左上角的位置
[segmentedControl setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];

//获取指定索引选项的图片imageForSegmentAtIndex:
UIImage *image = [segmentedControl imageForSegmentAtIndex:1];

//获取指定索引选项的标题titleForSegmentAtIndex
NSString *title = [segmentedControl titleForSegmentAtIndex:0];

//获取总选项数segmentedControl.numberOfSegments
NSInteger number = segmentedControl.numberOfSegments;

//获取指定索引选项的宽度widthForSegmentAtIndex:
CGFloat width = [segmentedControl widthForSegmentAtIndex:2];

//设置指定索引选项不可选
// [segmentedControl setEnabled:NO forSegmentAtIndex:4];

//判断指定索引选项是否可选
// BOOL enableFlag = [segmentedControl isEnabledForSegmentAtIndex:4];

[self.segmentedControl addTarget:self action:@selector(didClicksegmentedControlAction:) forControlEvents:UIControlEventValueChanged];


- (void)didClicksegmentedControlAction:(UISegmentedControl *)segmentedControl {
    NSInteger index = segmentedControl.selectedSegmentIndex;
    switch (index) {
        case 0:
            // code
            break;
        case 1:
            // code
            break;
        default:
            break;
    }
}

你可能感兴趣的