UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[segmentedControl imageForSegmentAtIndex:1]];
UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 30.0, 30.0)];
UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(140.0, 100.0, 30.0, 30.0)];
numberOfSegments.text = [NSString stringWithFormat:@"%d",segmentedControl.numberOfSegments];
UILabel *widthForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(180.0, 100.0, 70.0, 30.0)];
widthForSegmentAtIndex.text = [NSString stringWithFormat:@"%f",[segmentedControl widthForSegmentAtIndex:2]];
//cap insets用来指定哪些区域是固定不变的,未制定的区域则会repeat
UIImage *segmentSelected = [[UIImage imageNamed:@"bg_o.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImage *segmentUnselected = [[UIImage imageNamed:@"bg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
UIImage *segmentSelectedUnselected = [UIImage imageNamed:@"line.png"] ;
UIImage *segUnselectedSelected = [UIImage imageNamed:@"line.png"] ;
UIImage *segmentUnselectedUnselected = [UIImage imageNamed:@"line.png"];
//Segmente未选中背景
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
//Segmente选中背景
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
//Segmente左右都未选中时的分割线
//BarMetrics表示navigation bar的状态,UIBarMetricsDefault 表示portrait状态(44pixel height),UIBarMetricsLandscapePhone 表示landscape状态(32pixel height)
[[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
//字体
NSDictionary *textAttibutesUnSelected = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:18],UITextAttributeFont,
[UIColor blackColor],UITextAttributeTextColor,
[UIColor whiteColor],UITextAttributeTextShadowColor,
[NSValue valueWithCGSize:CGSizeMake(1, 1)],UITextAttributeTextShadowOffset,nil];
NSDictionary *textAttibutesSelected = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:18],UITextAttributeFont,
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor whiteColor],UITextAttributeTextShadowColor,
[NSValue valueWithCGSize:CGSizeMake(0, 0)],UITextAttributeTextShadowOffset,nil];
[[UISegmentedControl appearance] setTitleTextAttributes:textAttibutesUnSelected
forState:UIControlStateNormal];
[[UISegmentedControl appearance] setTitleTextAttributes:textAttibutesSelected
forState:UIControlStateSelected];