如何更改段分隔符颜色?

时间:2017-01-06 12:08:56

标签: ios objective-c uisegmentedcontrol

在我的项目中,我正在使用分段控制器..它们在我的分段控制器中有四个分段。我的问题是我想要这种背景颜色和字体颜色以及状态选择颜色和分隔符颜色[选择分段时为白色]

喜欢这个图片

Expected segment Controller

但是我的屏幕是

My Original screen

我的代码是

- (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];


}

我的代码我将尝试更改背景颜色并更改字体大小

2 个答案:

答案 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并不复杂,因为您必须为imagesselected州提供deselected,为image提供1px separator

我将建议您使用四种不同的buttons并应用逻辑,一次只选择一个button

要获得separator颜色效果,请将这四个buttons放在stack view或普通UIView内,并将该容器视图的背景颜色设置为您的颜色想要设置separator