Iphone uibarbuttonite亮显色调/颜色
我们是否可以在选中uibarbuttonite并将其高亮显示时更改其颜色/色调?我正在创建的应用程序将经常在户外使用,并希望它在高眩光情况下更明显,以便用户知道他实际上按下了按钮Iphone uibarbuttonite亮显色调/颜色,iphone,objective-c,uibarbuttonitem,Iphone,Objective C,Uibarbuttonitem,我们是否可以在选中uibarbuttonite并将其高亮显示时更改其颜色/色调?我正在创建的应用程序将经常在户外使用,并希望它在高眩光情况下更明显,以便用户知道他实际上按下了按钮 编辑:我想更改按钮高亮显示状态的颜色我还没有尝试过此操作,但是您可以尝试对UIBarButtonItem进行子类化,然后覆盖touchSedend:withEvent:和touchSeStart:withEvent:方法,然后使用它们为UIBarButtonItem实例设置tintColor 您需要将其添加到UIBar
编辑:我想更改按钮高亮显示状态的颜色我还没有尝试过此操作,但是您可以尝试对
UIBarButtonItem
进行子类化,然后覆盖touchSedend:withEvent:
和touchSeStart: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];
}
我没有尝试过这一点,但您可以尝试子类化
UIBarButtonItem
,然后覆盖touchSedend:withEvent:
和touchSeBegind: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];
}
UIButton是UIControl的一个子类,具有adjustsImageWhenHighlighted、showsTouchWhenHighlighted和showsTouchWhenHighlighted属性
uibarbuttonite是UIBarItem的一个子类,没有这些。但是,它确实有一个uiBarButtonimStyle,当设置为uiBarButtonimStyleplain时,表示按钮在点击时发光(但不能指定颜色) UIButton是UIControl的一个子类,具有adjustsImageWhenHighlighted、showsTouchWhenHighlighted和showsTouchWhenHighlighted属性
uibarbuttonite是UIBarItem的一个子类,没有这些。但是,它确实有一个uiBarButtonimStyle,当设置为uiBarButtonimStyleplain时,表示按钮在点击时发光(但不能指定颜色) 您可以将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;
}
您可以将uiBarButtonim子类化,并将uiButtonim放在其中,使用不同状态的不同背景图像颜色
- (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;
}
有可能,但当用户将手指从屏幕上提起,从而恢复到原始颜色时,不会触碰末端吗?我希望在代码执行之前保持新颜色。这可以作为一种退步,至少可以更好地显示用户确实按下了按钮……好吧,在这种情况下,只需从我的示例中省略
touchesend:withEvent:
。?我想在方法的末尾,我可以将tintColor重置为正常。。。我会尝试一下,谢谢。可能的话,但是当用户将手指从屏幕上提起,从而恢复到原来的颜色时,不会碰到末端吗?我希望在代码执行之前保持新颜色。这可以作为一种退步,至少可以更好地显示用户确实按下了按钮……好吧,在这种情况下,只需从我的示例中省略touchesend:withEvent:
。?我想在方法的末尾,我可以将tintColor重置为正常。。。我试试看,谢谢。