iOS 7中的UISegmentedControl

时间:2014-02-11 14:32:38

标签: ios7 uisegmentedcontrol

我有一个带有两个分段控件的项目。它们都适用于iOS7.0。现在一个人没有。我已经阅读了tintColor的问题,但我认为这是不同的。

两个控件都使用UIImages作为段。一,图像都正确显示。另一方面,我得到所有蓝色图像。

我做错了什么还是这个错误?

以下是故障段的代码:

UISegmentedControl *colorControl = [[UISegmentedControl alloc] initWithItems:
    [NSArray arrayWithObjects:
    [UIImage imageNamed:@"White.png"],
    [UIImage imageNamed:@"Red.png"],
    [UIImage imageNamed:@"Yellow.png"],
    [UIImage imageNamed:@"Green.png"],
    [UIImage imageNamed:@"Blue.png"],
    [UIImage imageNamed:@"Purple.png"],
     [UIImage imageNamed:@"Black.png"], nil]];



    CGRect frame = CGRectMake(rect.origin.x + kLeftMargin, rect.size.height - kPaletteHeight - kTopMargin, rect.size.width - (kLeftMargin + kRightMargin), kPaletteHeight );
    colorControl.frame = frame;

    // Add DoubleTap Color capability

    gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showMoreColors:)];
    [gesture setNumberOfTapsRequired:2];

    [colorControl addGestureRecognizer:gesture];

    // When the user chooses a color, the method changeColor: is called.
    [colorControl addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventValueChanged];

    // Make sure the color of the color complements the black background
    //colorControl.tintColor = [UIColor clearColor];

    // Add the control to the window
    [self.view addSubview:colorControl];

虽然图像全部为蓝色,但细分可按预期工作。

1 个答案:

答案 0 :(得分:0)

感谢this question,我制定了以下修复方法:

-(void) buildColorBar {
    //NSLog(@"%s", __FUNCTION__);

    UIImage *whiteImage = [UIImage imageNamed:@"White.png"];
    UIImage *blackImage = [UIImage imageNamed:@"Black.png"];
    UIImage *purpleImage = [UIImage imageNamed:@"Purple.png"];
    UIImage *redImage = [UIImage imageNamed:@"Red.png"];
    UIImage *blueImage = [UIImage imageNamed:@"Blue.png"];
    UIImage *greenImage = [UIImage imageNamed:@"Green.png"];
    UIImage *yellowImage = [UIImage imageNamed:@"Yellow.png"];

    NSArray *colorArray = [[NSArray alloc] initWithObjects:
                    [whiteImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal],
                    [redImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal ],
                    [yellowImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal ],
                    [greenImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal ],
                    [blueImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal ],
                    [purpleImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal ],
                    [blackImage imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal ], nil ];


    UISegmentedControl *colorControl = [[UISegmentedControl alloc] initWithItems:colorArray];

    CGRect frame = CGRectMake(rect.origin.x + kLeftMargin, rect.size.height - kPaletteHeight - kTopMargin, rect.size.width - (kLeftMargin + kRightMargin), kPaletteHeight );
    colorControl.frame = frame;

我希望它可以帮助别人。