UISegmentedControl仅在重新访问ViewController时更改文本颜色 [英] UISegmentedControl only changes text color when revisiting ViewController

查看:99
本文介绍了UISegmentedControl仅在重新访问ViewController时更改文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新已回答。由我。

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

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.

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

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

这是AppDelegate.m中的代码:

Here's the code within AppDelegate.m:

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

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

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

就像我说的最后一行有效,但只有当你 re时-visit 页面。为什么会发生这种情况?

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

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

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

推荐答案

找到答案:简单地移动

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

到您的AppDelegate.m文件

to your AppDelegate.m file

这篇关于UISegmentedControl仅在重新访问ViewController时更改文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆