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

Chi*_*isx 6 objective-c uisegmentedcontrol uicolor ios

更新回答.由我.

目前我的文字颜色变化有问题UISegmentedControl; 它需要在第一次加载时更改UIControlStateSelected.代码有效,但只是有条件的.当您在导航栏上使用分段控件访问页面,点击后退按钮,然后再次访问该页面时,它可以正常工作.我假设这里有继承问题.让我解释..

分段控件的位置位于导航栏的顶部.

包含SegmentedControl的ViewController的继承: TabBarViewController(使用AppDelegate管理) - >导航控制器 - > ViewController(其中'inviteSegBar'所在的位置)

这是AppDelegate.m中的代码:

[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:@"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

这里是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];
}
Run Code Online (Sandbox Code Playgroud)

就像我说的最后一行有效,但只有当你重新访问该页面时.为什么会这样?

PS这是同样的问题,我已经在列出任何答案之前尝试过此代码.

Chi*_*isx 9

答案:简单地移动

[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#669900"]} forState:UIControlStateSelected]; 
Run Code Online (Sandbox Code Playgroud)

到您的AppDelegate.m文件


Akh*_*jtr 6

使用

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];
Run Code Online (Sandbox Code Playgroud)

更新

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];
Run Code Online (Sandbox Code Playgroud)


Iva*_*vyi 5

此代码允许您在分段控件中为标签设置一些文本属性:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                            [UIColor blackColor], UITextAttributeTextColor,
                            nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
Run Code Online (Sandbox Code Playgroud)

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