ios分段控件的使用UISegmentedControl

 时间:2026-02-15 18:53:08

1、首先新建一个对象

 UISegmentedControl *segment = [[UISegmentedControl alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 2 - 50, 31, 100 , 25)];

  

2、设定对象分段控件的颜色

  segment.tintColor = [UIColor whiteColor];

    

3、插入两个元素

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

    [segment insertSegmentWithTitle:@"分析" atIndex:1 animated:YES];

    segment.selectedSegmentIndex = 0;

   

4、 //一旦你创建好分段控件之后,就可以将其赋予一个视图控制器的titleView导航属性,从而将其显示出来。这会使得标准的标题文字被替换成你的自定义视图:

 self.navigationItem.titleView = segmentedControl;

  

5、添加分段控件的点击方法,注意触发时机是UIControlEventValueChanged

  [segment addTarget:self action:@selector(handleSegmentAction:) forControlEvents:UIControlEventValueChanged];

    

6、还有问题,欢迎联系me。

  • Dev控件separatorControl控件属性详解
  • C# TabControl控件的用法
  • gridControl中单元格中的控件怎么取值
  • MFC设置picture control控件背景色
  • barCodeControl控件的属性以及用法介绍
  • 热门搜索
    广州白云山旅游攻略 快穿攻略黑化boss极致宠 节食减肥法 杭州一日游攻略 健康减肥早餐 轩辕剑之天之痕攻略 生殖器疱疹怎么传染 起亚狮跑怎么样 腹部减肥最快的方法 过午不食减肥法