Vous pouvez sous-classer UIBarButtonItem et y mettre UIButton avec différentes couleurs d'images d'arrière-plan pour différents états.
- (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;
}
Vous devez mettre cela en catégorie 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;
}
possible, mais ne serait pas touchesEnded fin lorsque l'utilisateur lève son doigt de l'écran et de revenir ainsi en arrière à la couleur d'origine? où je cherche à garder la nouvelle couleur jusqu'à ce que le code soit exécuté. cela pourrait fonctionner comme une retombée, au moins pour montrer que l'utilisateur a appuyé sur le bouton. – Padin215
... OK alors dans ce cas omettez 'touchesEnded: withEvent:' de mon exemple ...? –
Je suppose qu'à la fin de la méthode, je pourrais réinitialiser la tintColor à la normale ... Je vais essayer, merci. – Padin215