當我們選擇它時,我們是否可以更改uibarbuttonitem的顏色/色調並突出顯示?我正在創建的應用程序將經常使用外部門,並希望在高眩光情況下它更明顯,讓用戶知道他實際上按下了按鈕。uibarbuttonitem高亮色調/顏色
編輯:我想改變顏色的按鈕的高亮顯示狀態
當我們選擇它時,我們是否可以更改uibarbuttonitem的顏色/色調並突出顯示?我正在創建的應用程序將經常使用外部門,並希望在高眩光情況下它更明顯,讓用戶知道他實際上按下了按鈕。uibarbuttonitem高亮色調/顏色
編輯:我想改變顏色的按鈕的高亮顯示狀態
我沒有試過,但你可以嘗試繼承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];
}
一個UIButton是UIControl的子類,它有一個adjustsImageWhenHighlighted,一個showsTouchWhenHighlighted和showsTouchWhenHighlighted財產。
UIBarButtonItem是UIBarItem的子類,沒有這些。它確實有一個UIBarButtonItemStyle,當它被設置爲UIBarButtonItemStylePlain時,它表示點擊時按鈕發光(但不能指定顏色)。
您可以繼承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;
}
可能的,但是當用戶擡起手指離開屏幕,從而恢復到原來的顏色不會touchesEnded結束了嗎?在那裏我正在尋找保持新的顏色,直到代碼執行。這可以起到退回的作用,至少可以更好地表明用戶確實按下了按鈕。 – Padin215
...好的,在這種情況下,只需從我的示例中省略'touchesEnded:withEvent:'...? –
我想在方法結束時,我可以重置tintColor到正常...我會試一試,謝謝。 – Padin215