【iOS开发】UISegmentedControl自定义背景颜色和字体颜色

在UISegmentedControl的控件中,有一个设置默认颜色的

// The tintColor is inherited through the superview hierarchy. See UIView for more information. 
@property(null_resettable,nonatomic,strong) UIColor *tintColor; 

使用方式如下:

 [_segmentedControl setTintColor:[UIColor whiteColor]];
  • 设置选中时的背景色
[_segmentedControl setBackgroundImage:[UIImage imageNamed:@"selectImg"]
                          forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
  • 设置为选中时的背景色
[_segmentedControl setBackgroundImage:[UIImage imageNamed:@"unSelectImg"]
                                     forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  • 设置选中时字体颜色
 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:RGBA(.94, .31, .18),UITextAttributeTextColor,nil];
[_segmentedControl setTitleTextAttributes:dic forState:UIControlStateSelected];
  • 设置默认字体颜色
NSDictionary *dics = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil];
[_segmentedControl setTitleTextAttributes:dics forState:UIControlStateNormal];

你可能感兴趣的:(【iOS开发】UISegmentedControl自定义背景颜色和字体颜色)