3

我将这些图标用作工具栏中的栏按钮项目
http://stopiransnukes.org/images/home_icon.png

http://www.bladmuziekplus.nl/shop/files/images/icon_home.png
但背景图像已转换即使它是黑色的,也会自动变为白色。

我尝试通过代码设置图像背景,但仍然变成白色。

手动:我已将上面的图像拖放到项目中。我选择了条形按钮项目,并在右侧栏(盾牌图标)上的第 4 个选项上选择不同的色调,并将条形按钮下拉图像设置为上面的图像.

以编程方式:

  [self.homeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];

   [[self.homeButton] setImage:[UIImage imageNamed:@"home_icon.png"]];

我如何解决它 ?
提前致谢。

.

4

4 回答 4

2

UIButton这是添加图像并将其添加UIButtoncustom view内部的UIBarButtonItem逐步过程storyboard。我将添加一些图像让您完全理解这一点。

toolbar1 步:拖放storyboard视图 在视图中拖放工具栏

第 2 步:拖放UIButton到您的view

在此处输入图像描述

第 3 步:将图像拖放到您的UIButton

在此处输入图像描述

第 4 步:拖放UIButton工具栏中的 ,现在您可以在右侧将其添加到UIBarButtonItem. 现在你只需要为你的按钮连接一个动作方法。

在此处输入图像描述

于 2013-01-06T14:54:34.260 回答
1

这是正常行为,您可以在人机界面指南中阅读。

http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW8

如果你想改变,你需要编写自己的子类。

于 2013-01-06T12:44:37.753 回答
1

viewDidLoad

[super viewDidLoad];

//uıbarButtonItem firstButtonItem,secondButtonItem
UIImage* firstButtonImage = [UIImage imageNamed:@"firstImageName"];;
CGRect frameimg = CGRectMake(0, 0, 30, 30);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:firstButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(goToUserProfile)
     forControlEvents:UIControlEventTouchUpInside];
self.firstButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];


UIImage* secondButtonImage = [UIImage imageNamed:@"secondImageName"];
frameimg = CGRectMake(0, 0, 30, 30);
someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:secondButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(likePhoto)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setTag:1];
self.secondButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];

[self.toolbar setItems:[self addItemsToToolbar]];

AddItemsToToolbar 方法

- (NSMutableArray *)addItemsToToolbar{
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[items addObject:flexSpace];
[items addObject:self.firstButtonItem];
[items addObject:flexSpace];
[items addObject:self.secondButtonItem];
return items;

}

于 2013-01-06T14:36:07.420 回答
0
UIImage* infoButtonImage = [UIImage imageNamed:@"info_button.png"];;
CGRect frameimg = CGRectMake(0, 0, infoButtonImage.size.width, infoButtonImage.size.height);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:infoButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(actionOfButton)
     forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];

此代码将解决您的问题

于 2013-01-06T13:09:28.013 回答