0
votes

Xcode, Obj-c, iOS 5.2 iOS 6.0

I have a UISegmentedControl which is being inserted into the bottom toolbar using something like this:

-(void) makeSegmentedControl {
    self.SegControl = [[UISegmentedControl alloc] initWithItems:@[@"Title1",@"Title2"]];
    self.SegControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [self.SegControl setTarget:self action:@selector(handleSegment) forControlEvents:UIControlEventValueChanged]
    [self.SegControl setSelectedSegmentIndex:0];
 }
 -(void) AddSegmentedControlToToolbar {
     UIBarButtonItem *tmp = [UIBarButtonItem new];
     [tmp setCustomView:self.SegControl];
     [self.navigationController.toolbar setItems@[tmp]];
 }
 -(void) tintSegmentedControl {
     for (int i = 0; i< [self.SegControl.subviews count]; i++) {
          [[self.SegControl.subviews objectAtIndex:i] setTint:[UIcolor greenColor];
      }
      NSArray * sortedViews = [self.SegControl.subviews sortArrayUsingFunction:sortFunction];
      [[sortedViews objectAtIndex:self.SegControl.selectedSegmentIndex] setTintColor:[UIColor lightGreenColor]];
      for (id view in self.SegControl.subviews) {
          [view removeFromSuperView];
      }
      for (id view in sortedViews) {
          [self.SegControl addSubview:view];
      }
 }
 -(void)viewDidLoad {
     [self makeSegmentedControl];
     [self addSegmentedControlToToobar];
     [self tintSegmentedControl];
  } 
  -(void) handleSegment {
      [self tintSegmentedControl];
  }

When I run the similar code in our app it shows a very BLUE UISegmentedController which turns GREEN on clicking. I've tried a few things and it seems that the tint on the UISegments just refuses to stick until after the view has finished loading. Any idea what's happening here?

Edit

So, it turns out I was actually using the iOS 6.0 simulator and there is a restriction in iOS 6 that you can't tint a UISegmentedController before ViewDidAppear:Animated: has run. I think that this was the problem, because when I make this change:

-(void)viewDidLoad {
     [self makeSegmentedControl];
     [self addSegmentedControlToToobar];
} 
-(void)viewDidAppear {
     [NSTimer timerWithTimeInterval:0 target:self selector:@selector(tintSegmentedControl) userInfo:nil repeats:NO];
}

it works. Not 100% sure what's going on. Is there an easier way?

2

2 Answers

1
votes

try this code

-(void) tintSegmentedControl {
    [self.SegControl setTintColor:[UIColor greenColor];
      NSArray * sortedViews = [self.SegControl.subviews sortArrayUsingFunction:sortFunction];
      [[sortedViews objectAtIndex:self.SegControl.selectedSegmentIndex] setTintColor:[UIColor lightGreenColor]];
      for (id view in self.SegControl.subviews) {
          [view removeFromSuperView];
      }
      for (id view in sortedViews) {
          [self.SegControl addSubview:view];
      }
 }
0
votes

So, it turns out there is a restriction in iOS 6 that you can't tint a UISegmentedController before ViewDidAppear:Animated: has run. To circumvent it I made this code change (as you can see in the edit).

-(void)viewDidLoad {
     [self makeSegmentedControl];
     [self addSegmentedControlToToobar];
} 
-(void)viewDidAppear {
     [NSTimer timerWithTimeInterval:0 target:self selector:@selector(tintSegmentedControl) userInfo:nil repeats:NO];
}