UISegmentedControl only changes the color of the text when viewing the ViewController again

UPDATE ANSWER. Via ME.

There are currently problems with changing the text color of my UISegmentedControl ; it must be changed on first boot using UIControlStateSelected . The code works, but only conditionally. It works when you visit a page using the segmented control in the navigation bar, click the back button, and then go back to the page. I assume that there is a problem with inheritance. Let me explain ..

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

Inheritance of the ViewController that contains the SegmentedControl: TabBarViewController (controlled by AppDelegate) -> Navigation controller -> ViewController (where `inviteSegBar 'is located)

Here is the code in AppDelegate.m:

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

And here is the viewDidLoad: code viewDidLoad: for a VC that 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]; } 

As I said, the last line works, but only when you go to the page. Why is this happening?

PS These are the same problems, I already tried this code before any of the answers was specified.

+6
ios objective-c uicolor uisegmentedcontrol
Jan 16 '14 at 8:05
source share
5 answers

ANSWER TO FIND: just move

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

to the file AppDelegate.m

+8
Jan 19 '14 at 4:48
source share

Using

 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]; 
+6
Jan 16 '14 at 8:09
source share

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

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

Additional attributes in Apple documentation: link

+5
Jan 16 '14 at 8:08
source share

This may help you:

UIAppearance proxy for setting title text attributes, but keep tintColor for borders.

 [[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateNormal]; 
+2
Jan 16 '14 at 8:29
source share

To change the appearance of the UISegmentedControl interface, for example, into the 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]]; 
+1
Jan 16 '14 at 9:32
source share



All Articles