【问题标题】:UISegmentedControl tint color on touchUISegmentedControl 触摸时的色调颜色
【发布时间】:2010-04-10 11:57:21
【问题描述】:

我的应用中有一个 UISegmentedControl(参见下面的代码):

// --------------- SETTING NAVIGATION BAR RIGHT BUTTONS
NSArray *segControlItems = [NSArray arrayWithObjects:[UIImage imageNamed:@"up.png"],[UIImage imageNamed:@"down.png"], nil];
segControl = [[UISegmentedControl alloc] initWithItems:segControlItems];
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.momentary = YES;
segControl.frame = CGRectMake(25.0, 7, 65.0, 30.0);
segControl.tintColor = [UIColor blackColor];
[segControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];

if (current == 0) [segControl setEnabled:NO forSegmentAtIndex:0];
if (current == ([news count]-1)) [segControl setEnabled:NO forSegmentAtIndex:1];
// ---------------

但是当你点击它时我不能让它显示一些东西......

它在功能上完美运行,但我希望当你点击它时它会变成灰色(但只是在你点击时)......这可能吗?

谢谢,

哥蒂。

【问题讨论】:

    标签: iphone objective-c colors uisegmentedcontrol touchscreen


    【解决方案1】:

    似乎所选片段的色调颜色比原始色调更暗。因此,如果您的色调是黑色,那么所选片段的色调颜色不会改变,因为没有比这更暗的了。

    我环顾四周,没有找到任何好的方法来控制此控件的选定段的色调颜色。

    【讨论】:

      【解决方案2】:

      创建自己的分段控件的方法,(我猜)可以根据需要进行配置:

      idevrecipes custom segmented control

      顺便说一句:一个非常有趣的网站!

      【讨论】:

        【解决方案3】:

        试试这个。如果您使用分段标题,它可以工作,如果您使用图像,则可能需要修改它。

        segControl.segmentedControlStyle = UISegmentedControlStyleBar;
        [segControl addTarget:self action:@selector(changeColors:) forControlEvents:UIControlEventValueChanged];    
        
        -(IBAction)changeColors:(UISegmentedControl *)sc{
            for (int i=0; i<sc.numberOfSegments; i++) {
                // reset all the titles in the segmented control
                [sc setTitle:[NSString stringWithFormat:@"%i", i] forSegmentAtIndex:i];
            }
        
            if (sc.selectedSegmentIndex < 0 || sc.selectedSegmentIndex >= sc.numberOfSegments) {
                return;
            }
        
            CGFloat width = sc.frame.size.width / sc.numberOfSegments - 1;
            CGRect rect = CGRectMake(0, 0, width, sc.frame.size.height - 2);
        
            UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
            CGContextRef ctx = UIGraphicsGetCurrentContext();
        
            // background gradient
            UIColor *colorOne = [UIColor colorWithWhite:0.6 alpha:1.0]; //top of gradient
            UIColor *colorTwo = [UIColor colorWithWhite:0.4 alpha:1.0]; //bottom of gradient
            NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
            CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
            CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
            CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,rect.size.height), 0);
        
            // black shadow at the top
            colors = [NSArray arrayWithObjects:(id)[UIColor colorWithWhite:0.0 alpha:0.3].CGColor, (id)[UIColor clearColor].CGColor, nil];
            gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
            CGContextDrawLinearGradient(ctx, gradient, CGPointMake(0,0), CGPointMake(0,2.0), 0);
        
            UILabel * title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, sc.frame.size.height - 4)];
            title.text = [sc titleForSegmentAtIndex:sc.selectedSegmentIndex];
            title.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
            title.textAlignment = UITextAlignmentCenter;
            title.textColor = [UIColor whiteColor];
            title.backgroundColor = [UIColor clearColor];
            title.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
            title.shadowOffset = CGSizeMake(0, -0.5);
            [title.layer renderInContext:ctx];
        
            UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            [sc setImage:img forSegmentAtIndex:sc.selectedSegmentIndex];
        }
        

        【讨论】:

          猜你喜欢
          • 2015-04-21
          • 1970-01-01
          • 1970-01-01
          • 2018-03-01
          • 1970-01-01
          • 2010-12-31
          • 1970-01-01
          • 2016-07-28
          • 1970-01-01
          相关资源
          最近更新 更多