【问题标题】:Segmented control tintColor in iOS 6iOS 6 中的分段控制 tintColor
【发布时间】:2012-09-28 05:05:48
【问题描述】:

我有一个 8 段的分段控件。我可以更改整个控件的默认色调,但是我可以为控件中的每个段设置不同的颜色吗?我找到了一个在 5.1 中使用的教程,其中包含一个调用此方法的新类,

-(void)setTintColor:(UIColor*)color forTag:(NSInteger)aTag{}

但它在 iOS 6 中不起作用。有什么想法吗?

【问题讨论】:

    标签: ios6 uisegmentedcontrol tintcolor


    【解决方案1】:

    此问题已在此处修复。由于格式问题,我无法粘贴源代码。 Sample code here.

    编辑:添加了来自链接的注释和代码并修复了格式。 〜奥利耶

    这是一个 hacky 修复。这将起作用。将您的代码放在 ViewDidAppear 中。这样就可以了。

    - (void)viewDidAppear:(BOOL)animated 
    {
        [super viewDidAppear: animated];
        dispatch_async(dispatch_get_main_queue(), ^{
            for (int i = 0 ; i < [segmentControl.subviews count] ; i++)
            {
                if ([[segmentControl.subviews objectAtIndex: i] isSelected] )
                {
                    [[segmentControl.subviews objectAtIndex: i] setTintColor: [UIColor blackColor]];
                    break;
                }
            }
        }); 
    }
    

    【讨论】:

    • 我想给 + 100。我为此浪费了七个小时。看到你的代码后,它的工作很棒。 @barryjones。继续黑客攻击
    【解决方案2】:

    您可以为每个片段设置不同的片段图像和颜色。您可以使用的颜色:

    //get the subviews of the segmentedcontrol
    
    NSArray *arri = [segmentedControl subviews];
    
    //change the color of every subview(segment) you have
    
    [[arri objectAtIndex:0] setTintColor:[UIColor redColor]];
    
    [[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];
    

    希望能解决问题。

    【讨论】:

    • 嗨 Saalis,这适用于 5.1,但不适用于新的 6.0。认为这是我的项目,我创建了一个新的空白项目,上面只有一个分段控件并添加了这段代码。它在 5.1 模拟器上运行良好,但在 6.0 模拟器上运行良好。有什么想法吗?
    【解决方案3】:

    你是对的... iOS 6 不支持分段控制的子视图....

    我有一个替代方案:

    CGRect rect = CGRectMake(0, 0, 80, 44);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   [[UIColor redColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [segment setImage:img forSegmentAtIndex:0];
    

    您需要将核心图形框架添加到项目中。

    我们可以在 index.... 处为段绘制图像。但是如果您使用它,您将无法使用段标题添加文本。您还需要在上面使用的图像“img”上绘制文本。 如果您有其他方法,请分享。

    【讨论】:

      【解决方案4】:

      UiSegmentedControl 有一个属性 'segmentedControlStyle'(在 iOS7 中已弃用),它会影响 'tintColor' 的行为

      可能的样式有:

      UISegmentedControlStylePlain,   
      UISegmentedControlStyleBordered,
      UISegmentedControlStyleBar,     
      UISegmentedControlStyleBezeled, 
      

      但实际上在 iOS6 中“Bezeled”(已弃用)等于“Bar”

      前两种样式无法更改已应用“tintColor”,要对其进行自定义,您需要使用以下方法更改每个段的图像:

      - (void)setImage:(UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
      

      这样你将获得一个完全自定义的分段控件

      但如果默认值足以满足您的设计,您可以使用样式

      UISegmentedControlStyleBar
      

      “tintColor”属性将生效,您将获得一个彩色分段控件,根据所选分段应用色调以及让系统使用它进行拨号的所有其他好处。

      【讨论】:

      • 感谢线程中的最佳答案。像这样进行自定义将有助于支持 ios 7.x 和 6.x 到现在。
      【解决方案5】:

      这是一个设置红色并兼容 iOS 6 的简单解决方案。

      for ( UIView *segmentView in [segmentedControl subviews] ) {
          if ( [segmentView respondsToSelector:@selector(setTintColor:)] ) {
              [segmentView performSelector:@selector(setTintColor:)
                                withObject:[UIColor redColor]];
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-09-24
        • 2012-09-22
        • 2014-03-05
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        相关资源
        最近更新 更多