uibarbuttonitem突出色调/颜色

时间:2011-11-01 15:16:07

标签: iphone objective-c uibarbuttonitem

当我选择它并突出显示时,我们可以更改uibarbuttonitem的颜色/色调吗?我正在创建的应用程序将经常在门外使用,并希望在高眩光情况下更明显,以便用户知道他实际按下了按钮。

编辑:我想更改按钮突出显示状态的颜色

3 个答案:

答案 0 :(得分:0)

你可以将UIBarButtonItem子类化,并将UIButton放在其中,使用不同状态的不同背景图像颜色。

- (id)initWithImage:(UIImage *)image target:(id)target action:(SEL)action
{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = (CGRect){CGPointZero, image.size};

    [button setBackgroundImage:image forState:UIControlStateNormal];

    UIImage *highlightedImage = [image imageWithColor:[UIColor textHighlightColor]];
    [button setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
    [button setBackgroundImage:highlightedImage forState:UIControlStateSelected];

    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    self = [self initWithCustomView:button];

    return self;

}

你需要把它放到UIImage类别中:

- (UIImage *)imageWithColor:(UIColor *)color
{

    // begin a new image context, to draw our colored image onto
    UIGraphicsBeginImageContext(self.size);

    // get a reference to that context we created
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);

    // translate/flip the graphics context (for transforming from CG* coords to UI* coords
    CGContextTranslateCTM(context, 0, self.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // set the blend mode to color burn, and the original image
    CGContextSetBlendMode(context, kCGBlendModeMultiply);
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextDrawImage(context, rect, self.CGImage);

    // set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
    CGContextClipToMask(context, rect, self.CGImage);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context,kCGPathFill);

    // generate a new UIImage from the graphics context we drew onto
    UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    //return the color-burned image

    return coloredImg;

}

答案 1 :(得分:-1)

我没有试过这个,但您可以尝试继承UIBarButtonItem,然后重写touchesEnded:withEvent:touchesBegan:withEvent:方法,然后使用它们为您设置tintColor UIBarButtonItem实例。

您可以将其添加到UIBarButtonItem子类:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.tintColor = [UIColor redColor];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    self.tintColor = [UIColor blueColor];
}

答案 2 :(得分:-1)

UIButton是UIControl的子类,它具有adjustsImageWhenHighlighted,showsTouchWhenHighlighted和showsTouchWhenHighlighted属性。

UIBarButtonItem是UIBarItem的子类,没有这些子类。但它有一个UIBarButtonItemStyle,当设置为UIBarButtonItemStylePlain时,它表示按钮在点击时发光(但你不能指定颜色)。