UISegmentedControl with custom color: separator line bug

后端 未结 2 467
栀梦
栀梦 2021-01-06 02:53

Its easy change the color of UISegmentedControl. I found various solution like this, this site and the best this solution. But none was what I want.

I tried create a

相关标签:
2条回答
  • 2021-01-06 03:12

    I Think there is a lot easier solution. Just clean the pointers..

    for (int i=0; i<[self.segmentedControll.subviews count]; i++) 
    {
        [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:nil];
        if (![[self.segmentedControll.subviews objectAtIndex:i]isSelected]) 
        {   
            UIColor *tintcolor=[UIColor blackColor];
            [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor];
        } 
        else 
        {
            UIColor *tintcolor=[UIColor blueColor];
            [[self.segmentedControll.subviews objectAtIndex:i] setTintColor:tintcolor];
        }
    }
    
    0 讨论(0)
  • 2021-01-06 03:17

    Wow... When we write were in stackoverflow, we can calm down and think better. I write the answer in my ow question:

    If I go from right to left it not happens.

    This is the solution! What I have to do? Simulate this.

    BOOL inside = FALSE;
    - (void)changeColor{ 
        int old = segmentedControl.selectedSegmentIndex;
        for (int i=0; i<3; i++) {
          [segment[i] setTintColor:[UIColor lightGrayColor]];
        }  
    /**/inside = TRUE;
    /**/for (int i=3; i>0; i--) {
    /**/     //When I do this, it call "segmentedControl:"
    /**/    [segmentedControl setSelectedSegmentIndex:i];
    /**/}
    
    /**/int select = old;
        [segment[select] setTintColor:[UIColor blueColor]];     
        [segmentedControl setSelectedSegmentIndex:select];    
    /**/inside = FALSE;    
    } 
    
    - (void)segmentedControl:(id)sender 
    {
    /**/if (inside==TRUE) return;   //Ignore this calls.
        //do some thing
        [self changeColor];
    }
    
    0 讨论(0)
提交回复
热议问题