问题添加图像使用的UIBarButtonItem工具栏上,显示空白的白盒代替图像的
-
26-09-2019 - |
题
林不知道什么即时通讯做错了。文件名是正确的,样式设置为平原。但是我得到一个银行白盒我的图像的大小。即时通讯使用的UINavigationController。
请帮助和感谢你感谢你提前。
** FYI我八九不离十新目标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];
解决方案
它被创建白掩模的原因是因为所述UIToolBar
犯规默认允许在其上的彩色图像。的方式做到这一点是建立一个UIImage
然后分配给UIButton
该图像。然后创建一个使用UIBarButton
与initWithCustomView
作为自定义视图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];
其他提示
与iOS 7开始您可以使用下面:
UIImage *image = [[UIImage imageNamed:@"myImage.png"];
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)];
不通道引导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:)];
或只是检查项目; - )
不隶属于 StackOverflow