【问题标题】:iphone ios7 segmented UISegmentedControl change only border coloriphone ios7分段UISegmentedControl仅更改边框颜色
【发布时间】:2015-04-08 00:38:59
【问题描述】:

一直在四处寻找并尝试仅更改边框颜色(使用不同的文本颜色),但没有成功。可以更改色调,但同时更改文本和边框。

【问题讨论】:

    标签: ios uisegmentedcontrol


    【解决方案1】:

    您可以使用 UIAppearance 代理设置标题文本属性,但保留边框的 tintColor。 比如:

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

    编辑:

    要给图像着色,你可以在 UImage 的类别中使用类似的东西:

    - (instancetype)tintedImageWithColor:(UIColor *)tintColor {
        UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        CGRect rect = (CGRect){ CGPointZero, self.size };
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        [self drawInRect:rect];
    
        CGContextSetBlendMode(context, kCGBlendModeSourceIn);
        [tintColor setFill];
        CGContextFillRect(context, rect);
    
        UIImage *image  = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    【讨论】:

    • 做得很好。快速,正确的答案。请人们,加上这个答案。
    • 我会说我在这里的回答是一个更正确的解决方案。随意看看:)
    • 只有在分段控件中使用文本时才有效。当项目是图像时,它们会被染成与边框相同的颜色,这种方法无济于事。
    • 没错。但是手动着色图像相当容易,我已经更新了我的答案。
    • @KamilKocemba 干得好!但是如果我想改变页面图像段的颜色我该怎么做呢?我用[self.segmentControl insertSegmentWithImage:[[UIImage imageNamed:@"myImage.png"] tintedImageWithColor:[UIColor redColor]] atIndex:i animated:NO];
    【解决方案2】:

    当您为UISegmentedControl 设置图像时,我更喜欢不同的解决方案(无类别),而不是像这样更改图像:

    NSArray *items = nil;
    if (NSFoundationVersionNumber>NSFoundationVersionNumber_iOS_6_1) {
      items = @[
              [[UIImage imageNamed:@"Images_Icon_Notes.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal],
              [[UIImage imageNamed:@"Images_Icon_Keywords.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal],
              [[UIImage imageNamed:@"Images_Icon_Actionitems.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal],
              [[UIImage imageNamed:@"Images_Icon_Questions.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]
              ];
    } else {
      items = @[
              [UIImage imageNamed:@"Images_Icon_Notes.png"],
              [UIImage imageNamed:@"Images_Icon_Keywords.png"],
              [UIImage imageNamed:@"Images_Icon_Actionitems.png"],
              [UIImage imageNamed:@"Images_Icon_Questions.png"]
              ];
    }
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
    segmentedControl.tintColor = [UIColor greenColor]; // desired color of border
    

    现在tintColor 将只影响边框而不影响图标。

    if 提供与旧 iOS 版本的兼容性。
    我必须说imageWithRenderingMode: 是我见过的最伟大的 API WTF 之一。

    【讨论】:

    • 原始问题不是关于着色图像。
    • “仅更改边框颜色” by tintColor。但是默认情况下这会对图标产生影响,这可以防止这个问题。我认为这很明显,显然我错了,所以我进行了更新。接受的答案使用类别作为解决方法来解决相同的问题。我的方法要好得多。
    • 现在我注意到你威胁图标的方式与我预期的不同。我的回答仍然很有用。
    【解决方案3】:

    对我有用的是:正如其他答案所暗示的,将 segmentedControl 的 tintColor 更改为 clearColor。并手动将图像着色为您的应用着色颜色。

    记得在着色图像上使用imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

    -(UIImage *)tintedImage:(UIImage *)image withColor:(UIColor *)tintColor
    {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGRect rect = (CGRect){ CGPointZero, image.size };
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    [image drawInRect:rect];
    
    CGContextSetBlendMode(context, kCGBlendModeSourceIn);
    [tintColor setFill];
    CGContextFillRect(context, rect);
    
    UIImage *newImage  = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    

    【讨论】:

      【解决方案4】:

      您可以先更改 tintColor,然后更改 titleText。

      //Changes Tint Color
      segmentedControlName.tintColor = [UIColor colorWithRed:0/255 green:0/255 blue:0/255 alpha:1];
      
      //TitleText to BlackColor
      [[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] } forState:UIControlStateNormal];
      

      【讨论】:

        【解决方案5】:

        我的解决方案给分段控件的边框另一种颜色,并将文本保持为淡色。

        为了只更改分段控件的边框颜色,请将另一个分段控件放在旧控件之上。然后禁用这个新的用户交互,并将所选段的图像设置为零。

        UISegmentedControl *segCtrl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segm 1", @"Segm 2", @"Segm 3", @"Segm 4", nil]]; 
        // The UISegmentedController which you want to change the border color for
        
        [segCtrl setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)];
        [segCtrl setSelectedSegmentIndex:0];
        [segCtrl setTintColor:[UIColor redColor]];
        
        
        UISegmentedControl *bcg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@" ", @" ", @" ", @" ", nil]]; 
        // The UISegmentedController you put on top of the other one
        
        [bcg setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)];
        [bcg setSelectedSegmentIndex:0];
        [bcg setImage:nil forSegmentAtIndex:0]; // Removing highlight color
        [bcg setTintColor:[UIColor greenColor]];
        [bcg setUserInteractionEnabled:NO];
        
        
        
        [[self view] addSubview:segCtrl];
        [[self view] addSubview:bcg];
        




        当然,您必须注意框架和颜色以适合您的应用程序。除此之外,这段代码应该很好用。

        【讨论】:

        • a)这是错误的,请参阅另一个更简单明了的答案 b)您对固定计算的控件外观做出了疯狂的假设。对 iOS 的一个小改动,你将不得不更新你的代码。 c) 除非绝对需要,否则不要覆盖两个控件。
        【解决方案6】:
         for (int i=0; i<[mySegmentedControl.subviews count]; i++)
            {
                if ([[mySegmentedControl.subviews objectAtIndex:i]isSelected] )
                {
                [[mySegmentedControl.subviews objectAtIndex:i] setBackgroundColor:[UIColor blueColor]];
                 [[mySegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor clearColor]];
                 }
        
                 else
                 {
                [[mySegmentedControl.subviews objectAtIndex:i] setBackgroundColor:[UIColor whiteColor]];
                 [[mySegmentedControl.subviews objectAtIndex:i] setTintColor:[UIColor clearColor]];
                 }
        }
        

        【讨论】:

          猜你喜欢
          • 2013-10-11
          • 2016-04-17
          • 2013-10-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多