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

更新回答.由我.

目前我的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]];

这里是viewDidLoad:VC的代码,其中包含’inviteSegBar’,即所讨论的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文件

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...