当重新访问视图控制器时,UISegmentedControl仅更改文本颜色

6

更新,我的回答如下。

目前我遇到了一个问题,就是无法改变我的UISegmentedControl的文本颜色;它需要在第一次加载时使用UIControlStateSelected进行更改。虽然代码可以运行,但只有特定情况下才能正常工作。当你访问带有分段控件的导航栏页面,点击返回按钮,再重新访问该页面时,代码才会正常工作。我认为这里存在继承问题。让我解释一下……

分段控件位于导航栏顶部。

包含分段控件的视图控制器的继承关系:TabBarViewController(由AppDelegate管理)-->导航控制器-->ViewController(其中包含“inviteSegBar”)

下面是AppDelegate.m中的代码:

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

以下是包含“inviteSegBar”的视图控制器的viewDidLoad:代码,其中问题所在的是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];
}

就像我之前说的那样,最后一行代码是有效的,但仅在您重新访问页面时才有效。为什么会这样呢?

附言: 同志们,这是同一个问题,我在任何答案列出之前已经尝试过这段代码。

5个回答

12

找到答案了:只需移动

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

到你的AppDelegate.m文件


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

更新

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

1
UITextAttributeTextColor 在 iOS7.0 中首次被弃用。 - Chisx

5

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

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

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


2
这可能会对你有帮助:
使用UIAppearance代理设置标题文本属性,但保留tintColor用于边框。
[[UISegmentedControl appearance] setTitleTextAttributes:@{ 
NSForegroundColorAttributeName : [UIColor redColor] 
} forState:UIControlStateNormal];

好的,这个可以工作,但是不是立即的。基本上,我从我的导航栏中经历了一些极其奇怪的行为。我已经在这个问题的编辑中发布了问题。 - Chisx
你想在导航栏按钮上执行两个不同的操作吗?你可以在那里放置两个barbuttonitems。这将方便地自定义导航栏和按钮。 - A J
很抱歉,我更喜欢使用 segmentedControl 来完成这个任务。它可以在不同的 tableViews 之间切换。虽然我知道两种方法都可以实现,但我更倾向于使用分段控件。 - Chisx

1

要更改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]];

这段代码仍然只有在重新访问视图时更改了 UISegmentedControl,问题仍然存在。 - Chisx

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接