更改 iOS 工具栏中的默认图标颜色

发布于 2024-12-05 20:03:02 字数 365 浏览 0 评论 0原文

我需要工具栏中的自定义图标(黄色工具栏背景上的黑色图标)。 我尝试了 UIBarButtonItem initWithImage 构造函数,但在这种情况下 图标是用alpha值显示的,好像没办法 更改基本的白色图标颜色。我最终使用了 UIButton,但它会好得多, 仅更改默认图标颜色,可以吗?

UIImage *buttonImage = [UIImage imageNamed:iconName];

UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStylePlain target:nil action:nil];

I need custom icons in toolbar (black icons on yellow toolbar background).
I tried UIBarButtonItem initWithImage constructor, but it this case
is icon displayed using alpha values, and it seems that there is no way
to change basic white icon color. I ended up using UIButton, but it will be much better,
to just change default icon color, is it possible?

UIImage *buttonImage = [UIImage imageNamed:iconName];

UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStylePlain target:nil action:nil];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-12-12 20:03:02

您可以通过设置色调使工具栏变成黄色:

toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];

如果您想要自定义 UIBarButtonItem 具有自定义图像、颜色等...就像您在 UIButton 中一样,一种选择是创建一个类,将 UIButton 封装为 UIBarButtonItem 中的自定义视图。这是我的自定义课程 - 希望它有所帮助:

@interface ENBarButtonImageItem : UIBarButtonItem
{
     UIButton *_button;
}

@implementation ENBarButtonImageItem

- (id)initWithFrame:(CGRect)frame 
              image:(UIImage*)image 
    backgroundImage:(UIImage*)bgImage
{
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setFrame:frame];

    self = [super initWithCustomView:_button];
    if (self) 
    {
        if (image)
            [_button setImage:image forState:UIControlStateNormal];

        if (bgImage)
            [_button setBackgroundImage:bgImage forState:UIControlStateNormal];

    }
    return self;    
}

- (id)initWithFrame:(CGRect)frame 
              image:(UIImage*)image 
    backgroundImage:(UIImage*)bgImage 
             target:(id)target 
             action:(SEL)selector
{
    self = [self initWithFrame:frame image:image backgroundImage:bgImage];
    if (self)
    {
        [_button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
    [_button release];
}

- (void)addTarget:(id)target action:(SEL)selector forControlEvents:(UIControlEvents)controlEvents
{
    [_button addTarget:target action:selector forControlEvents:controlEvents];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [_button setImage:image forState:state];
}

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
{
    [_button setImage:image forState:state];
}

You can make the toolbar yellow by setting the tint:

toolbar.tintColor = [UIColor colorWithRed:0.83 green:0.43 blue:0.57 alpha:0.5];

If you want custom UIBarButtonItem with custom image, colors etc... like you get in UIButton, one option is to create a class which encapsulates UIButton as the custom view in UIBarButtonItem. Here's my custom class - hope it helps:

@interface ENBarButtonImageItem : UIBarButtonItem
{
     UIButton *_button;
}

@implementation ENBarButtonImageItem

- (id)initWithFrame:(CGRect)frame 
              image:(UIImage*)image 
    backgroundImage:(UIImage*)bgImage
{
    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    [_button setFrame:frame];

    self = [super initWithCustomView:_button];
    if (self) 
    {
        if (image)
            [_button setImage:image forState:UIControlStateNormal];

        if (bgImage)
            [_button setBackgroundImage:bgImage forState:UIControlStateNormal];

    }
    return self;    
}

- (id)initWithFrame:(CGRect)frame 
              image:(UIImage*)image 
    backgroundImage:(UIImage*)bgImage 
             target:(id)target 
             action:(SEL)selector
{
    self = [self initWithFrame:frame image:image backgroundImage:bgImage];
    if (self)
    {
        [_button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    }

    return self;
}

- (void)dealloc
{
    [super dealloc];
    [_button release];
}

- (void)addTarget:(id)target action:(SEL)selector forControlEvents:(UIControlEvents)controlEvents
{
    [_button addTarget:target action:selector forControlEvents:controlEvents];
}

- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
    [_button setImage:image forState:state];
}

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
{
    [_button setImage:image forState:state];
}
白首有我共你 2024-12-12 20:03:02

阅读initWithImage:style:target:action:参数说明

图片
该项目的图像。如果为零,则不显示图像。

栏上显示的图像源自此图像。如果该图像太大而无法放在条上,则会缩放以适合该图像。通常,工具栏和导航栏图像的大小为 20 x 20 点。源图像中的 Alpha 值用于创建图像 - 不透明值将被忽略。

除了 alpha 之外的所有内容都将被忽略。工具栏是黑白的。
如果您确实需要颜色,则可以子类化或使用其他 UI 框架,例如 Three20

Read description of parameters of initWithImage:style:target:action:

image
The item’s image. If nil an image is not displayed.

The images displayed on the bar are derived from this image. If this image is too large to fit on the bar, it is scaled to fit. Typically, the size of a toolbar and navigation bar image is 20 x 20 points. The alpha values in the source image are used to create the images—opaque values are ignored.

Everything except alpha is ignored. Toolbar is black and white.
Subclass or use other UI frameworks, like three20 if you really need colors there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文