Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

customize color for selected segmented in segmented control

How can I customize/change color for selected segmented in segmented control? I tried to use method available at UISegmentedControl selected segment color . It worked perfectly with iOS 5 and below but not for iOS 6. Any help is appreciated.

Basically I am looking to change color for the selected segmented to some bright color so that selected/unselected segments are clearly visible.

like image 230
user1140780 Avatar asked Oct 17 '12 03:10

user1140780


People also ask

How do I change text color in segmented control?

tintColor = [UIColor redColor]; for (id segment in [button subviews]) { for (id label in [segment subviews]) { if ([label isKindOfClass:[UILabel class]]) { UILabel *titleLabel = (UILabel *) label; [titleLabel setTextColor:[UIColor blackColor]]; } } } UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] ...

How do you change view controllers using segmented control?

To switch between the child view controllers, we use a segmented control. Click the + button in the top right to bring up the Library and add a segmented control to the navigation bar of the master view controller. Open MasterViewController. swift and create an outlet for the segmented control.


2 Answers

We used the approach mentioned by siddarth.

Subclass the segmented controller and overriding the drawrect() method. Something like this:

- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];

for (int i=0; i<[self.subviews count]; i++)
{
    if ([[self.subviews objectAtIndex:i]isSelected] )
    {
        UIColor *tintcolor=[UIColor redColor];
        [[self.subviews objectAtIndex:i] setTintColor:tintcolor];
    } else {
        UIColor *tintcolor=[UIColor grayColor]; // default color
        [[self.subviews objectAtIndex:i] setTintColor:tintcolor];
    }
   }

}
like image 199
user1140780 Avatar answered Sep 30 '22 13:09

user1140780


You can override the subclass of that particular view and then override its drawRect() method for its custom appearance on the screen.

like image 22
kidsid49 Avatar answered Sep 30 '22 12:09

kidsid49