在iOS 7下UISegmentedControl外观不仅与iOS 6不一样,而且iOS 7下UISegmentedControl的触摸状态也比iOS 6多了一种:高亮状态(UIControlStateHighlighted)。所以在iOS 6的时候,设置自定义外观需要添加高亮状态时的外观。
前面有篇文章介绍了在iOS 5以后可以用UIAppearance来全局设置外观。今天在项目碰到个问题:有两处使用UISegmentedControl的地方外观不一样,好在UIAppearance有appearanceWhenContainedIn:这个方法,可以很方便的实现需求。不过如果按照前面的文章里那样写,估计代码量不少。在这里我把公用的代码抽出,这样不但代码量减少,看起来也会相当清晰:
公用的代码如下:
-(void)segmentControlAppearanceContainedIn:(Class <UIAppearanceContainer>)ContainerClass
withMaterial:(NSDictionary *)material
{
id appearance = [UISegmentedControl appearanceWhenContainedIn:ContainerClass, nil];
//文本
[appearance setTitleTextAttributes:[material objectForKey:@"TextAttributes_Highlighted"]
forState:UIControlStateHighlighted];
[appearance setTitleTextAttributes:[material objectForKey:@"TextAttributes_Selected"]
forState:UIControlStateSelected];
[appearance setTitleTextAttributes:[material objectForKey:@"TextAttributes_Normal"]
forState:UIControlStateNormal];
//文字位置
[appearance setContentPositionAdjustment:UIOffsetMake(0, 3)
forSegmentType:UISegmentedControlSegmentAny
barMetrics:UIBarMetricsDefault];
//背景
[appearance setBackgroundImage:[material objectForKey:@"Bg_Normal"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:[material objectForKey:@"Bg_Selected"]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
[appearance setBackgroundImage:[material objectForKey:@"Bg_Highlighted"]
forState:UIControlStateHighlighted
barMetrics:UIBarMetricsDefault];
//分割线
//Unselected | Unselected
[appearance setDividerImage:[material objectForKey:@"Divider_Unselected_Unselected"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
//Selected | Unselected
[appearance setDividerImage:[material objectForKey:@"Divider_Selected_Unselected"]
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
//Unselected | Selected
[appearance setDividerImage:[material objectForKey:@"Divider_Unselected_Selected"]
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
}
material是需要添加的素材:文本属性,背景,分割线等。ContainerClass表示你需要只在哪个类中实现这个外观,示例如下:
//文本
NSDictionary *dic_TextAttributes_Highlighted = @{
UITextAttributeTextColor: [UIColor grayColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(0, 0)],
UITextAttributeFont:[self SDFontWithFamilyName:_SubTitleFontName_ size:13.0f]
};
NSDictionary *dic_TextAttributes_Selected = @{
UITextAttributeTextColor: [UIColor whiteColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(0, 0)],
UITextAttributeFont:[self SDFontWithFamilyName:_SubTitleFontName_ size:13.0f]
};
NSDictionary *dic_TextAttributes_Normal = @{
UITextAttributeTextColor: [UIColor grayColor],
UITextAttributeTextShadowOffset:[NSValue valueWithCGSize:CGSizeMake(0, 0)],
UITextAttributeFont:[self SDFontWithFamilyName:_SubTitleFontName_ size:13.0f]
};
//背景
UIImage *bg_Normal = [[[UIImage imageNamed:@"whiteDot_1pix.png"]imageWithTintColor:[UIColor whiteColor]] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0,0)];
UIImage *bg_Selected = [[[UIImage imageNamed:@"whiteDot_1pix.png"] imageWithTintColor:[UIColor grayColor]] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *bg_Highlighted = [[[UIImage imageNamed:@"whiteDot_1pix.png"] imageWithGradientTintColor:HighLightColor_SegmentControl_In_Product] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
//分割线
UIImage *divider_U_U = [[UIImage imageNamed:@"bgSegmentDivider.png"] imageWithTintColor:[UIColor grayColor]];
UIImage *divider_S_U = [[UIImage imageNamed:@"bgSegmentDivider.png"] imageWithTintColor:[UIColor grayColor]];
UIImage *divider_U_S = [[UIImage imageNamed:@"bgSegmentDivider.png"] imageWithTintColor:[UIColor grayColor]];
NSDictionary *material = @{@"TextAttributes_Highlighted":dic_TextAttributes_Highlighted,
@"TextAttributes_Selected":dic_TextAttributes_Selected,
@"TextAttributes_Normal":dic_TextAttributes_Normal,
@"Bg_Normal":bg_Normal,
@"Bg_Selected":bg_Selected,
@"Bg_Highlighted":bg_Highlighted,
@"Divider_Unselected_Unselected":divider_U_U,
@"Divider_Selected_Unselected":divider_S_U,
@"Divider_Unselected_Selected":divider_U_S};
[self segmentControlAppearanceContainedIn:nil withMaterial:material];
虽然看起来还是很多,但是都是必须的,以后类似其它的视图如果也有多种全局外观需要设置,方便起见,也可以这样写。
上面的代码是iOS 6和iOS同时UISegmentedControl外观设置。如果只在iOS7进行外观设置,是非常简单的,代码如下:
id appearance = [UISegmentedControl appearance];
[appearance setTintColor:[UIColor grayColor]];
appearance = [UISegmentedControl appearanceWhenContainedIn:[UINavigationBar class], nil];
[appearance setTintColor:[UIColor whiteColor]];