首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >uibarbuttonitem高亮显示tint/color

uibarbuttonitem高亮显示tint/color
EN

Stack Overflow用户
提问于 2011-11-01 23:16:07
回答 3查看 4.6K关注 0票数 2

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-02 00:48:15

我还没有尝试过这样做,但是您可以尝试将UIBarButtonItem子类化,然后覆盖touchesEnded:withEvent:touchesBegan:withEvent:方法,然后使用它们来设置UIBarButtonItem实例的tintColor

您可以将以下代码添加到UIBarButtonItem子类中:

代码语言:javascript
复制
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    self.tintColor = [UIColor redColor];
}

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

Stack Overflow用户

发布于 2014-03-24 12:05:48

你可以创建UIBarButtonItem的子类,并将UIButton放入其中,为不同的状态提供不同的背景图像颜色。

代码语言:javascript
复制
- (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类别中:

代码语言:javascript
复制
- (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;

}
票数 0
EN

Stack Overflow用户

发布于 2011-11-02 00:59:48

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

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7968619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档