UISegmetControl

时间:2023-03-09 18:34:31
UISegmetControl

一、UISegmentControl是一个分段控件,父类是UIControl,内涵数个button,并且都有对应下标index;

NSArray *titles = @[@"护卫队",@"三军仪仗队",@"步兵方队",@"炮兵方队"];

UISegmentedControl  *SegmentedC = [[UISegmentedControl alloc]initWithItems:titles];

SegmentedC.frame = CGRectMake(20, 40, 335, 40);

SegmentedC.backgroundColor = [UIColor whiteColor];

//    设置文字颜色

SegmentedC.tintColor = [UIColor redColor];

//    [SegmentedC setTintColor:[UIColor whiteColor]];

//    默认选中项

SegmentedC.selectedSegmentIndex =0;

//    修改某项的title

[SegmentedC setTitle:@"女兵方阵" forSegmentAtIndex:2];

//    指定某项宽度

[SegmentedC setWidth:100 forSegmentAtIndex:1];

//    设置title  文字大小   颜色

NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor blueColor]};

NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor yellowColor]};

[SegmentedC setTitleTextAttributes:dic forState:UIControlStateNormal];

[SegmentedC setTitleTextAttributes:dic1 forState:UIControlStateSelected];

//    添加点击事件

[SegmentedC addTarget:self action:@selector(handleSegmented:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:SegmentedC];

[SegmentedC release];