当uibarbuttonitem被选中并突出显示时,我们可以更改它的颜色/色调吗?我正在创建的应用程序将经常在户外使用,并希望它在高眩光情况下更明显,让用户知道他实际上按下了按钮。
编辑:我想要更改按钮突出显示状态的颜色
发布于 2011-11-02 00:48:15
我还没有尝试过这样做,但是您可以尝试将UIBarButtonItem子类化,然后覆盖touchesEnded:withEvent:和touchesBegan:withEvent:方法,然后使用它们来设置UIBarButtonItem实例的tintColor。
您可以将以下代码添加到UIBarButtonItem子类中:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.tintColor = [UIColor redColor];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.tintColor = [UIColor blueColor];
}发布于 2014-03-24 12:05:48
你可以创建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;
}发布于 2011-11-02 00:59:48
UIButton是UIControl的子类,它具有adjustsImageWhenHighlighted、showsTouchWhenHighlighted和showsTouchWhenHighlighted属性。
UIBarButtonItem是UIBarItem的子类,并且没有这些。但是,它确实有一个UIBarButtonItemStyle,当设置为UIBarButtonItemStylePlain时,表示按钮在被点击时会发光(但你不能指定颜色)。
https://stackoverflow.com/questions/7968619
复制相似问题