简体   繁体   English

UISegmentedContol更改背景色而不更改图像色调颜色

[英]UISegmentedContol change background color without changing image tint color

I have a UISegmentedContol with images inside each segment. 我有一个UISegmentedContol,每个段内都有图像。 In the storyboard I set the background color and the tint color of the image to white. 在情节提要中,我将背景颜色和图像的色调设置为白色。 Now when one segment is selected, I am trying to change the background color only and keep the image tint to white (the same as the unselected segments) but I am unable to do so. 现在,当选择一个片段时,我试图仅更改背景颜色并将图像色调保持为白色(与未选择的片段相同),但是我无法这样做。 Here is my code: 这是我的代码:

- (IBAction)onSegmentValueChanged:(id)sender
{
    UISegmentedControl *segment = (UISegmentedControl*)sender;
    UIColor *tintcolor=UIColorFromRGB(0x3A0F3A);

    for (int i=0; i<[segment.subviews count]; i++)
    {
        if ([[segment.subviews objectAtIndex:i]isSelected] )
        {
            [[segment.subviews objectAtIndex:i] setBackgroundColor:[UIColor purpleColor]];
        [[segment.subviews objectAtIndex:i] setTintColor:[UIColor whiteColor]];
        }
        else
        {
            [[segment.subviews objectAtIndex:i] setBackgroundColor:tintcolor];
        }
    }    
}

See picture below: 见下图:

https://www.dropbox.com/s/y93m3vtgr95e2dy/IMG_9249.PNG?dl=0 https://www.dropbox.com/s/y93m3vtgr95e2dy/IMG_9249.PNG?dl=0

The unselected segments have the correct background color (purple)and the correct image tint (white). 未选择的段具有正确的背景颜色(紫色)和正确的图像色调(白色)。 The selected segment should have a purple background with a white image tint but instead it has a white background. 所选的片段应具有紫色背景并带有白色图像色调,但应具有白色背景。

Thanks 谢谢

For all additional functions with segment control, i'm using custom segment control. 对于具有段控制的所有其他功能,我正在使用自定义段控制。 For example you can check my segment control. 例如,您可以检查我的细分控件。

https://github.com/Bambaster/CustomSegmentControl https://github.com/Bambaster/CustomSegmentControl

So you can add your images, colors and etc. 因此,您可以添加图像,颜色等。

I had the similar need in my previous project and I used the PPiFlatSegmentedControl for it and it works like magic. 在之前的项目中,我也有类似的需求, 为此我使用了PPiFlatSegmentedControl ,它的工作原理就像魔术一样。

Thanks, 谢谢,

Attiqe 阿提克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM