使用 UIBarButtonItem 将图像添加到工具栏时出现问题,显示空白白框而不是图像

发布于 2024-09-28 20:25:11 字数 525 浏览 1 评论 0原文

我不确定我做错了什么。文件名正确,样式设置为plain。但我得到了一个与我的图像大小相同的银行白盒子。我正在使用 UINavigationController。

请协助并提前致谢。

**仅供参考,我对 Objective C 有点陌生,所以不要对我太严厉。 ;)

 UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:[UIImage imageNamed:@"channel-guide-button.png"]
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];


self.toolbarItems = [NSArray arrayWithObjects:toolbarChannelGuideButton, nil];
[toolbarChannelGuideButton release];

Im not sure what im doing wrong. The file name is correct, the style is set to plain. But Im getting a bank white box the size of my image. Im using UINavigationController.

Please assist and thank you thank you in advance.

**FYI I am sorta new to objective c so dont be too hard on me. ;)

 UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:[UIImage imageNamed:@"channel-guide-button.png"]
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];


self.toolbarItems = [NSArray arrayWithObjects:toolbarChannelGuideButton, nil];
[toolbarChannelGuideButton release];

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

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

发布评论

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

评论(3

乄_柒ぐ汐 2024-10-05 20:25:11

它创建白色蒙版的原因是 UIToolBar 默认情况下不允许在其上显示彩色图像。实现此目的的方法是创建一个 UIImage,然后为该图像分配一个 UIButton。然后使用 initWithCustomView 创建一个 UIBarButton,并将 UIButton 作为自定义视图。

代码:

     //Load the image   
     UIImage *buttonImage = [UIImage imageNamed:@"your-image.png"];

     //create the button and assign the image
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:buttonImage forState:UIControlStateNormal];

     //sets the frame of the button to the size of the image
     button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

     //creates a UIBarButtonItem with the button as a custom view
     UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];



     self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
     [customBarItem release];

The reason it was creating the white mask was because the UIToolBar doesnt allow color images on it by default. The way to accomplish this is creating a UIImage then assign a UIButton to that image. Then create a UIBarButton using initWithCustomView with the UIButton as the custom view.

Code:

     //Load the image   
     UIImage *buttonImage = [UIImage imageNamed:@"your-image.png"];

     //create the button and assign the image
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     [button setImage:buttonImage forState:UIControlStateNormal];

     //sets the frame of the button to the size of the image
     button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);

     //creates a UIBarButtonItem with the button as a custom view
     UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];



     self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
     [customBarItem release];
┊风居住的梦幻卍 2024-10-05 20:25:11

从 iOS 7 开始,您可以使用以下内容:

 UIImage *image = [[UIImage imageNamed:@"myImage.png"];
 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];

Starting with iOS 7 you can use below:

 UIImage *image = [[UIImage imageNamed:@"myImage.png"];
 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
心如荒岛 2024-10-05 20:25:11

channel-guide-button.png 属于项目吗?

你可以这样解决:

UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"];
NSLog(@" image = %p", image);
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:image
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];

或者只是检查你的项目;-)

Does channel-guide-button.png belong to project?

You could break this out like this:

UIImage *image = [UIImage imageNamed:@"channel-guide-button.png"];
NSLog(@" image = %p", image);
UIBarButtonItem *toolbarChannelGuideButton = [[UIBarButtonItem alloc]
     initWithImage:image
     style:UIBarButtonItemStylePlain
     target:self
     action:@selector(action:)];

or just check your project ;-)

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