我从免费图标DB下载了一个拇指向上的图标,它的大小对于一个按钮太大而且它有白色背景。我决定在pixelmator中打开它,调整图标大小并删除背景。当用预览打开它时它看起来很棒但是当它被设置为xcode中的按钮图像时它显示为白色圆圈。
这是编辑后的图标(30x30 png) 这就是按钮
的外观我该怎么做才能解决这个问题?
编辑/更新: 我尝试了@yunas代码,它在导航栏中工作不适用于工具栏(甚至没有显示按钮)。
尝试此代码:
UIImage* image = [UIImage imageNamed:@"imageName"];
UIBarButtonItem *likeBtn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(action_clicked:)];
NSArray* toolbarItems = [NSArray arrayWithObjects:likeBtn,nil];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
再次出现了一个带有白色绘图的按钮
所以我想知道:工具栏中是否有可能有彩色按钮?也许我正在尝试不可能......
编辑/ upadte II - 解决方案:将UIButton包装在UIBarButtonItem中
@interface StatusUIBarButtonItem : UIBarButtonItem
- (id) initWithStatus: (enum StatusEnum)p_statusEnum;
@end
@implementation StatusUIBarButtonItem
- (instancetype)initWithStatus:(enum StatusEnum)p_statusEnum
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[self getStatusImage] forState:UIControlStateNormal];
[button addTarget:self action:@selector(status_Clicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 20, 20)];
self = [super initWithCustomView:button];
if (self)
{
self.statusButton = button;
}
return self;
}
@end
然后在ViewController类中:
-(void) viewDidLoad
{
StatusUIBarButtonItem* stausBtn = [[StatusUIBarButtonItem alloc] initWithStatus:self.status];
NSArray* toolbarItems = [NSArray arrayWithObjects: stausBtn, nil];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
}
我希望这会帮助像我一样的人想要工具栏中的彩色图像
答案 0 :(得分:1)
UIImage *buttonImage = [UIImage imageNamed:@"imageName.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.adjustsImageWhenDisabled = NO;
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = customBarItem;