UISegmentedControl仅在重新访问ViewController时更改文本颜色

更新答复。 由我。

目前有我的UISegmentedControl的文字颜色变化的问题; 它需要改变与UIControlStateSelected第一次加载。 代码工作,但只有条件。 它在您使用导航栏上的分段控件访问页面时起作用,点击后退button,然后再次访问该页面。 我假设这里有inheritance问题。 让我解释..

分段控件的位置位于我的导航栏上方。

包含SegmentedControl的ViewController的inheritance: TabBarViewController(使用AppDelegatepipe理) – >导航控制器 – > ViewController(其中'inviteSegBar'在于)

以下是AppDelegate.m中的代码:

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

这里是viewDidLoad:包含“inviteSegBar”的VC的代码, UISegmentedControl有问题:

 - (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]; } 

就像我说的最后一行的作品,但只有当你重新访问页面。 为什么发生这种情况?

PS这是同样的问题的家伙,我已经在任何答案被列出之前已经试过这个代码。

答案:简单地移动

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

到你的AppDelegate.m文件

使用

 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]; 

更新

 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]; 

这段代码允许你在分段控制中为标签设置一些文本属性:

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

Apple文档中允许的更多属性: 链接

这可以帮助你:

UIAppearance代理设置标题文本属性,但保留tintColor的边界。

 [[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateNormal]; 

对于改变UISegmentedControl外观插入例如viewDidLoad函数这个代码:

 // 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]];