UISegmentedControl Color Issue - Color Shows Fine

2019-05-24 10:33发布

问题:

FIXED!

You won't believe it... It was a Winterboard theme messing with it! The bugger! Disabled theme and voila, works perfectly. The theme in question was "Ayecorn" for anyone interested. Not cool! Hope this helps anyone else who encounters the same issue.

Sorry folks, and thanks for all the input! Great community here.

Original Question:

So a simple test iPhone app in Xcode 4.5:

Create a view with a UISegmentedControl via Storyboard, and set the color via attributes inspector.

Run it on simulator, color shows fine:

Run it on device, color is see-thru/clear.

(Yes in this example above I have colored individual segments, but I even created a new project, added segment control to the view (Bar Type) and it came out see thru!? What gives?

Anyone experienced this before and have and advice on how to correct this?

Thanks

回答1:

Your simulator might have cached the older images you used before. Clean your simulator and entire project to make sure the images are properly loaded on your simulator.



回答2:

Try pasting below code

- (void)viewDidLoad
{
    UISegmentedControl *segmentControl=[[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"1",@"2",nil] ];
    [segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segmentControl setFrame:CGRectMake(20, 20, 200, 30)];

    [self.view addSubview:segmentControl];

    [segmentControl addTarget:self action:@selector(changeSegment:) forControlEvents:UIControlEventValueChanged];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)changeSegment:(UISegmentedControl*)sender
{
    for (int i=0; i<[sender.subviews count]; i++)
    {
        UIColor *tintcolor;
        if ([[sender.subviews objectAtIndex:i]isSelected] )
        {
            tintcolor=[UIColor redColor];
            [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
        }

        if (![[sender.subviews objectAtIndex:i]isSelected]){
            tintcolor=[UIColor grayColor];
            [[sender.subviews objectAtIndex:i] setTintColor:tintcolor];
        }
    }
}


回答3:

It's possible you have the Opaque option unchecked in the Drawing section of the View Attributes.

I vaguely recall having an issue similar to this where the behaviour on the emulator was different from that of the device and it was related to the Opaque setting.



回答4:

You won't believe it... It was a Winterboard theme messing with it! The bugger! Disabled theme and voila, works perfectly. The theme in question was "Ayecorn" for anyone interested. Not cool!

Thanks for all the input guys! Great community here!