在我的项目中,我正在使用分段控制器..它们在我的分段控制器中有四个分段。我的问题是我想要这种背景颜色和字体颜色以及状态选择颜色和分隔符颜色[选择分段时为白色]
喜欢这个图片
但是我的屏幕是
我的代码是
- (void)viewDidLoad {
[self changeColor];
}
- (void)changeColor{
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:83.0f/255.0f green:198.0f/255.0f blue:255.0f/255.0f alpha:1.0]} forState:UIControlStateSelected];
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:197.0f/255.0f green:197.0f/255.0f blue:197.0f/255.0f alpha:1.0]} forState:UIControlStateNormal];
[mailboxsegment setTintColor:[UIColor colorWithRed:202.0f/255.0f green:202.0f/255.0f blue:202.0f/255.0f alpha:1.0]];
UIFont *font = [UIFont boldSystemFontOfSize:09.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[mailboxsegment setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
我的代码我将尝试更改背景颜色并更改字体大小
答案 0 :(得分:2)
please try this one
- (void)segmentAction:(UISegmentedControl *)segment
{
UIColor *selectedColor = [UIColor whiteColor];
UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
for (UIControl *subview in [segment subviews])
{
if ([subview isSelected])
[subview setTintColor:selectedColor];
else
[subview setTintColor:deselectedColor];
}
}
答案 1 :(得分:0)
您可以创建自定义segmented control
。但创建自定义segmented control
并不复杂,因为您必须为images
和selected
州提供deselected
,为image
提供1px separator
等
我将建议您使用四种不同的buttons
并应用逻辑,一次只选择一个button
。
要获得separator
颜色效果,请将这四个buttons
放在stack view
或普通UIView
内,并将该容器视图的背景颜色设置为您的颜色想要设置separator
。