首页 > 代码库 > 一步一步学习IOS(UISegmentedControl) 分段控件的属性

一步一步学习IOS(UISegmentedControl) 分段控件的属性

 //创建分段按钮

    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

    //设置Item的宽度

    UIColor *MyTint = [[UIColor alloc] initWithRed:0.66 green:1.0 blue:0.77 alpha:1.0];

    segment.tintColor = MyTint;

    //添加片段

    [segment insertSegmentWithTitle:@"First" atIndex:0 animated:YES];

    [segment insertSegmentWithTitle:@"Two" atIndex:2 animated:YES];

    [segment insertSegmentWithImage:[UIImage imageNamed:@"ondemo"] atIndex:3 animated:YES];

    //删除分段

//    [segment removeSegmentAtIndex:2 animated:YES];//删除一个片段

//    [segment removeAllSegments];//删除所有片段

    //片段标题

    [segment setImage:[UIImage imageNamed:@"statrdemo"] forSegmentAtIndex:1];

    //选中分段

    segment.momentary = YES;

    //指定选中按钮

    segment.selectedSegmentIndex = 1;

    //设置背景颜色

    segment.backgroundColor = [UIColor puopleColor];

    //添加到导航栏,无导航栏时没有任何效果

    self.navigationItem.titleView = segment;

    //读取控件的分段按钮的值

    int x= segment.selectedSegmentIndex;

  //添加事件

    [segment addTarget:self action:@selector(selectedd: ) forControlEvents:UIControlEventEditingChanged];

    [self.view addSubview:segment];

 技术分享

一步一步学习IOS(UISegmentedControl) 分段控件的属性