【问题标题】:UISegmentedControl only changes text color when revisiting ViewControllerUISegmentedControl 仅在重新访问 ViewController 时更改文本颜色
【发布时间】:2014-02-05 00:56:28
【问题描述】:

更新已答复。由我。

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

这是 VC 的 viewDidLoad: 代码,其中包含“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这是同一个问题,在列出任何答案之前我已经尝试过这段代码。

【问题讨论】:

    标签: ios objective-c uisegmentedcontrol uicolor


    【解决方案1】:

    找到答案:只需移动

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

    到您的 AppDelegate.m 文件

    【讨论】:

      【解决方案2】:

      使用

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

      【讨论】:

      • UITextAttributeTextColor 在 iOS7.0 中首次弃用
      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

        这可能对你有帮助:

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

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

        【讨论】:

        • 好吧,这行得通,但不是立即生效。基本上我在导航栏中遇到了一些非常奇怪的行为。我已在此问题的 EDIT 中发布了该问题。
        • 您想要对导航栏按钮项目进行两种不同的操作吗?您可以在那里放置两个 barbuttonitem。这将轻松自定义导航栏和按钮。
        • 恐怕我宁愿只使用segmentedControl。它在tableViews 之间切换。我知道它会是一样的,我只是更喜欢分段控件。
        【解决方案5】:

        如需更改 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,问题仍然存在。
        猜你喜欢
        • 1970-01-01
        • 2012-10-17
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 1970-01-01
        相关资源
        最近更新 更多