简体   繁体   中英

UISegmentedContol change background color without changing image tint color

I have a UISegmentedContol with images inside each segment. 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

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

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.

Thanks,

Attiqe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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