UI UISegmentedControl分段控件

<span style="font-size:18px;">    //创建UISegmentedControl对象
    NSArray *items=[NSArray arrayWithObjects:@"首页",@"列表",@"收藏", nil];
    UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:items];
    //设置边框的颜色
    seg.tintColor=[UIColor redColor];
    //修改frame,将其移到想要的位置
    seg.frame=CGRectMake(20, 20, 280, 34);
    //插入一项内容
    [seg insertSegmentWithTitle:@"我的" atIndex:3 animated:YES];
    //设置默认选中的一个
    seg.selectedSegmentIndex=2;
    //添加事件
    [seg addTarget:self action:@selector(segClicked:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
    [seg release];
- (void)segClicked:(UISegmentedControl*)seg
{
    NSLog(@"segClicked:");
    //每次被点击时,从被点击项的后面添加一项
    [seg insertSegmentWithTitle:@"niew" atIndex:seg.selectedSegmentIndex+1 animated:YES];
}
</span>

你可能感兴趣的:(UI UISegmentedControl分段控件)