// typedef NS_ENUM(NSInteger, UISegmentedControlStyle) {
// UISegmentedControlStylePlain, // large plain
// UISegmentedControlStyleBordered, // large bordered
// UISegmentedControlStyleBar, // small button/nav bar style. tintable
// UISegmentedControlStyleBezeled, // DEPRECATED. Do not use this style.
// } NS_DEPRECATED_IOS(2_0, 7_0, "The segmentedControlStyle property no longer has any effect");
//在ios7 里UISegmentedControlStyle 这个属性不在有效果
NSArray * itemArray = [NSArray arrayWithObjects:@"祝福",@"吻别",@"傻瓜",nil];
segmentedControl = [[UISegmentedControl alloc]initWithItems:itemArray];
segmentedControl.frame =CGRectMake(100,100,180, 40);
//是否显示选中状态默认no
segmentedControl.momentary =NO;
//控件渲染色
UIColor *segmentedControltintColor = [[UIColor alloc]initWithRed:0.1green:1.0blue:123.88alpha:1.0];
segmentedControl.tintColor = segmentedControltintColor;
//分段控件的宽度
[segmentedControl setWidth:100forSegmentAtIndex:0];
//读取设置分段控件的宽度默认为 0
CGFloat itemWidth = [segmentedControl widthForSegmentAtIndex:0];
//分段控件的响应事件
[segmentedControl addTarget:selfaction:@selector(segmengAction:)forControlEvents:UIControlEventEditingChanged];
// For segments whose width value is 0, setting this property to YES attempts to adjust segment widths based on their content widths. Default is NO.
segmentedControl.apportionsSegmentWidthsByContent =NO;
//插入一个item
[segmentedControlin sertSegmentWithTitle:@"背包"atIndex:3animated:YES];
//插入一个带图片的item
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:@"itemImage.png"]atIndex:4animated:YES];
//设置一个图片的item
[segmentedControl setImage:[UIImage imageNamed:@"itemImage.png"] forSegmentAtIndex:2];
//读取一个item图片
UIImage *segmentedControlimageItem = [segmentedControl imageForSegmentAtIndex:2];
//调整图片或文本在分段控件上的偏移
CGSize textSize =CGSizeMake(1,5);
[segmentedControl setContentOffset:textSize forSegmentAtIndex:2];
//获取偏移量 默认0
CGSize textfloat = [segmentedControl contentOffsetForSegmentAtIndex:2];
//设置 item是否可以点击默认 yes
[segmentedControl setEnabled:NO forSegmentAtIndex:4];
// 读取item 是否可以点击
BOOL item4isbool = [segmentedControl isEnabledForSegmentAtIndex:4];
//删除一个item
[segmentedControl removeSegmentAtIndex:0animated:YES];
//删除说有item
// [segmentedControl removeAllSegments];
//设置Item标题
[segmentedControl setTitle:@"站台"forSegmentAtIndex:1];
// 读取item 的标题
NSString *titleString = [segmentedControl titleForSegmentAtIndex:1];
// 获取item 的索引号
int itemNumebr =segmentedControl.selectedSegmentIndex;
NSLog(@"title =%@",titleString);
//在导航栏中间
// self.navigationItem.titleView = segmentedControl;
[self.view addSubview:segmentedControl];
-(void)segmentAction:(UISegmentedControl *)Seg{
NSInteger Index = Seg.selectedSegmentIndex;
NSLog(@"Index %i", Index);
switch (Index) {
case 0:
break;
case 1:
break;
case 2:
break;
default:
break;
}
}