6

UPDATE ANSWERED. BY ME.

Currently having problems with the text color change of my UISegmentedControl; it needs to change on first load with UIControlStateSelected. Code works, but only conditionally. It works when you visit the page with the segmented control on the navigation bar, hit the back button, and then visit the page again. I'm assuming there's a problem with inheritance here. Let me explain..

The location of the the segmented control lies on top of my navigation bar.

Inheritance of the ViewController which contains the SegmentedControl: TabBarViewController(managed with AppDelegate)-->navigation Controller-->ViewController(where 'inviteSegBar' lies)

Here's the code within AppDelegate.m:

[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:@"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];

And here's the viewDidLoad: code for the VC which contains 'inviteSegBar', the UISegmentedControl in question:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //CUSTOM APPEARANCE <below>
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#669900"];

    inviteSegBar.tintColor = [UIColor colorWithHexString:@"#333333"];

    [[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#669900"]} forState:UIControlStateSelected];
}

Like I said the last line works, but only when you re-visit the page. Why is this happening?

PS This is the same issue guys, I had already tried this code before any of the answers were listed.

Chisx
  • 1,976
  • 4
  • 25
  • 53

5 Answers5

12

ANSWER FOUND: Simply move

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#669900"]} forState:UIControlStateSelected]; 

to your AppDelegate.m file

Chisx
  • 1,976
  • 4
  • 25
  • 53
6

Use

UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[whitecolor] forKeys:@[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
                                         forState:UIControlStateNormal];

UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[grayColor] forKeys:@[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
                                         forState:UIControlStateSelected];

update

UIColor *whitecolor = [UIColor whiteColor];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[whitecolor] forKeys:@[NSForegroundColorAttributeName]];
    [yourSegment setTitleTextAttributes:attributes
                                             forState:UIControlStateNormal];

    UIColor *grayColor = [UIColor darkGrayColor];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[grayColor] forKeys:@[NSForegroundColorAttributeName]];
    [yourSegment setTitleTextAttributes:attributes
                                             forState:UIControlStateSelected];
Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30
5

This code allows you to set some text attributes for label in segmented control:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                            [UIColor blackColor], UITextAttributeTextColor,
                            nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];

More allowed attributes in Apple documentation: link

Ivan Lisovyi
  • 538
  • 5
  • 13
2

This may help you:

UIAppearance proxy to set title text attributes but preserve tintColor for borders.

[[UISegmentedControl appearance] setTitleTextAttributes:@{ 
NSForegroundColorAttributeName : [UIColor redColor] 
} forState:UIControlStateNormal];
A J
  • 605
  • 4
  • 16
  • Alright this works, but not immediately. Basically I'm experiencing some extremely weird behavior from my navigation bar. I've posted the issue in the **EDIT** of this question. – Chisx Jan 16 '14 at 23:50
  • Do you want two different actions on navigation bar button item? You can place two barbuttonitems there. This will easy customise nav bar as well as button. – A J Jan 17 '14 at 06:52
  • I'm afraid I'd rather just use the `segmentedControl` for this. It's switching between `tableViews`. I understand that it would be the same, i just would prefer the segmented control. – Chisx Jan 17 '14 at 20:06
1

For change UISegmentedControl appearance insert for example into viewDidLoad function this code:

// color selected text ---> red
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];

// color disabled text ---> blue
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];

// color tint segmented control ---> black
[[UISegmentedControl appearance] setTintColor:[UIColor greenColor]];
Alessandro Pirovano
  • 2,509
  • 28
  • 21
  • This code is still only changing the `UISegmentedControl` when **revisiting** the view, problem still occurs. – Chisx Jan 17 '14 at 20:10