我将这些图标用作工具栏中的条形按钮项目
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"]];
我该如何解决? 在此先感谢。
答案 0 :(得分:2)
这是一个循序渐进的过程,将图片添加到UIButton
并将UIButton
添加为custom view
UIBarButtonItem
内的storyboard
..我会添加一些图片供你完全理解..
第1步:将toolbar
拖放到storyboard
视图中
第2步:将UIButton
拖放到view
第3步:将图片拖放到UIButton
第4步:将UIButton
拖放到工具栏中,现在您可以在右侧将其添加到UIBarButtonItem
内。现在你只需要为你的按钮连接一个动作方法。
答案 1 :(得分:1)
这是正常行为,您可以在人机界面指南中阅读。
如果你想改变它,你需要编写自己的子类。
答案 2 :(得分: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;
}
答案 3 :(得分: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];
此代码将解决您的问题