如何更改段分隔符颜色?

时间:2022-11-20 21:34:15

In my Project i am using segment Controller .. they have four segment in my segment Controller ..My question is I want this background color and font color and State selected color and separator color[White when select the segment]

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

Like this Image

喜欢这个图片

如何更改段分隔符颜色?.

But My screen is

但我的屏幕是

如何更改段分隔符颜色?

My code Is

我的代码是

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


}

my code i will try to change background color and change font size

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

2 个解决方案

#1


3  

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

#2


-1  

You can create your custom segmented control. But its little complicated to create custom segmented control as you will have to provide images for selected and deselected states, 1px image for separator etc.

您可以创建自定义分段控件。但是创建自定义分段控件并不复杂,因为您必须为选定和取消选择的状态提供图像,1px图像用于分隔符等。

Instead of this i will suggest you to use four different buttons and apply logic to select only one button at a time.

而不是这个,我建议您使用四个不同的按钮并应用逻辑一次只选择一个按钮。

To get effect of separator color, place these four buttons inside a stack view or a normal UIView and set background color of that container view to the color which you want to set for separator.

要获得分隔符颜色的效果,请将这四个按钮放在堆栈视图或普通UIView中,并将该容器视图的背景颜色设置为要为分隔符设置的颜色。

#1


3  

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

#2


-1  

You can create your custom segmented control. But its little complicated to create custom segmented control as you will have to provide images for selected and deselected states, 1px image for separator etc.

您可以创建自定义分段控件。但是创建自定义分段控件并不复杂,因为您必须为选定和取消选择的状态提供图像,1px图像用于分隔符等。

Instead of this i will suggest you to use four different buttons and apply logic to select only one button at a time.

而不是这个,我建议您使用四个不同的按钮并应用逻辑一次只选择一个按钮。

To get effect of separator color, place these four buttons inside a stack view or a normal UIView and set background color of that container view to the color which you want to set for separator.

要获得分隔符颜色的效果,请将这四个按钮放在堆栈视图或普通UIView中,并将该容器视图的背景颜色设置为要为分隔符设置的颜色。