Search code examples
iosobjective-cuisegmentedcontrol

UISegmentedControl changes tint color after dismissing


I have UISegmentedControl on my controller. Which tint color works fine when I am opening it for the first time. But on the back button pressed I will dismiss the controller. And when reopen the same controller, the tint color of UISegmentedControl disappear. Please help. Thanks in advance.

When open first time

When reopen after dismissing

headerSegment = [[UISegmentedControl alloc] initWithItems:segmentItemsArray];
headerSegment.selectedSegmentIndex = 0;
headerSegment.apportionsSegmentWidthsByContent = YES;
headerSegment.layer.borderWidth = 0;
headerSegment.layer.masksToBounds = NO;

UIFont *font = [UIFont systemFontOfSize:16];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                       forKey:NSFontAttributeName];
[headerSegment setTitleTextAttributes:attributes
                             forState:UIControlStateNormal];

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [ApplicationColors skyBlueColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateSelected];

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [ApplicationColors segmentNormalColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateNormal];

headerSegment.layer.borderColor = [UIColor clearColor].CGColor;
headerSegment.tintColor = [UIColor whiteColor];
headerSegment.backgroundColor = [ApplicationColors lightGrayBackgroundColor ];
headerSegment.translatesAutoresizingMaskIntoConstraints = NO;
[headerSegment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];

[contentHolderView addSubview:headerSegment];

Solution

  • Try with following code, (it works for me)

    //  ViewController.m
    
    #import "ViewController.h"
    
    @interface ViewController () {
        UISegmentedControl * headerSegment;    
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self addsegmentcontrol];
    }
    
    
    -(void)addsegmentcontrol{
    
    
        headerSegment = [[UISegmentedControl alloc] initWithItems:@[@"One", @"Two", @"Three"]];
        headerSegment.selectedSegmentIndex = 0;
        headerSegment.apportionsSegmentWidthsByContent = YES;
        headerSegment.layer.borderWidth = 0;
        headerSegment.layer.masksToBounds = NO;
    
        UIFont *font = [UIFont systemFontOfSize:16];
        NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                               forKey:NSFontAttributeName];
        [headerSegment setTitleTextAttributes:attributes
                                     forState:UIControlStateNormal];
    
        [headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateSelected];
    
        [headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor], NSFontAttributeName : [UIFont systemFontOfSize:16]} forState:UIControlStateNormal];
    
    
        headerSegment.layer.borderColor = [UIColor clearColor].CGColor;
        headerSegment.tintColor = [UIColor whiteColor];
        headerSegment.backgroundColor = [UIColor lightGrayColor];
        headerSegment.translatesAutoresizingMaskIntoConstraints = YES;
        //[headerSegment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
    
        [self.view addSubview:headerSegment];
         headerSegment.frame = CGRectMake(0, 200, 320, 44);
        [self.view layoutIfNeeded];
    }
    

    Here is sample result:

    enter image description here