UISegmentedControl 分裂的button

司寇昱
2023-12-01
//初始化  

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(10, 360, 300, 30)];

//设置标题

[segmentedControl insertSegmentWithTitle:@"我要买药" atIndex:0 animated:YES];

[segmentedControl insertSegmentWithTitle:@" 我要举报 " atIndex:1 animated:YES];

[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"u234_normal.png"] atIndex:2 animated:YES]; //设置显示图片

//设置样式

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;

//默认显示

[segmentedControl setSelectedSegmentIndex:0];

// 设置在点击后是否恢复原样

segmentedControl.momentary = YES;

//可触摸

segmentedControl.multipleTouchEnabled=NO;   

//点击事件

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

//把控件加入到页面里

[self.view addSubview:segmentedControl];

//增加到navigationbar上

//UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];  

//self.navigationItem.rightBarButtonItem = segButton;

//控件位置

//segmentedControl.center = CGPointMake(self.view.center.x - 50.0f, self.view.center.y);

//设置空间标题

[segmentedControlsetTitleTextAttributes:@{

                    UITextAttributeTextColor:[UIColorredColor],

//                         UITextAttributeFont:[UIFont fontWithName:@"KaiTi_GB2312" size:10],//有错误

     }forState:UIControlStateNormal];

//移除指定索引

[segmentedControl removeSegmentAtIndex:0 animated:NO];

//设置指定索引选项宽度

[segmentedControl setWidth:70.0f forSenmentAtIndex:0];

//获取指定索引选项的图片

UIImageView *imageForSegmentAtIndex = [UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];

imageForSegmentAtIndex.frame = CGRectMake(60.0,100.0,30.0,30.0);

//设置选项中图片左上角的位置

[segmentedControl setContentOffset:CGSizeMake(10.0, 10.0) forSegmentAtIndex:1];

//获取指定索引选项的标题

UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 30, 30)];

titleForSegmentAtIndex.text = [segmentedControl titleForSegmentAtIndex:1];

//总选项数    segmentedControl. numberOfSegments

segmentedControl.tintColor = [UIColor greenColor];//字体颜色

segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式

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

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

[segmentedControl removeAllSegments];//移除所有选项












 类似资料: