【问题标题】:Two buttons needed at navigationItem.rightBarButtonItem. Image not getting displayednavigationItem.rightBarButtonItem 需要两个按钮。图片不显示
【发布时间】:2010-03-07 23:00:04
【问题描述】:

我必须在导航栏的右侧有两个按钮。所以我创建了一个工具栏,并在其内容视图中添加了两个栏按钮项。如下所示。

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                    initWithImage:[UIImage imageNamed:@"share-icon-32x32.png"]
                                    style:UIBarButtonItemStyleBordered
                                    target:self action:@selector(showShare)];

    shareButton.width = 30.0f;
    [buttons addObject:shareButton];
    [shareButton release];

其中buttons是一个保存按钮对象的数组。

同样,我还有另一个条形按钮项目,如下所示

UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                      target:self
                                      action:@selector(addDescription)];

        addButton.style = UIBarButtonItemStyleBordered;
        [buttons addObject:addButton];
        [addButton release];

现在将数组添加到工具栏,如下所示

[toolbar setItems:buttons animated:YES];
    [buttons release];

并将其添加到 rightBarButton 如下所示

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithCustomView:toolbar];
    [toolbar release];

我在右侧栏上获得了两个栏按钮项目,但我无法将图像显示在共享按钮项目上。它只是一个白色的补丁。任何人都可以告诉我我做错了什么或如何显示图像。

问候,

赛优素福

【问题讨论】:

    标签: iphone


    【解决方案1】:

    来自initWithImage:style:target:action: 的文档:

    源图像中的 alpha 值用于创建图像 - 不透明值是 忽略。

    这意味着 UIKit 使用图像的 alpha 通道,而不是 RGB 像素值。您提供的图像可能完全不透明(没有透明度)并且 initWithImage:style:target:action: 将其解释为白色方块。您必须使用定义图标形状的 Alpha 通道保存图像。

    【讨论】:

    • 嗨 Begemann,非常感谢。是的,我使用的是不透明的图像。我使用了等效的灰度图像,它确实显示在导航栏中。问候,赛优素福
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2015-10-21
    • 2021-02-06
    相关资源
    最近更新 更多