[英]iOS Bar Item image displaying wrong color
使用UIBarButton的tintColor
設置圖像的所需顏色。 如果絕對需要使用原始圖像顏色,請使用此設置圖像:
[aBarButton setImage:[[UIImage imageNamed:@"xyz.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
文檔對此有點含糊不清
條形圖上顯示的圖像來自此圖像。 如果此圖像太大而無法放在條形圖上,則會縮放以適合該圖像。 通常,工具欄和導航欄圖像的大小為20 x 20磅。 源圖像中的alpha值用於創建圖像 - 忽略不透明值。
基本上這就是說你提供的圖像不是實際顯示的圖像。 相反,系統使用圖像的alpha蒙版和項目的tintColor
來生成最終顯示。
以編程方式添加圖像
[button setImage:[[UIImage imageNamed:@"imageName.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
如果它不起作用,那么試試這個: -
UIImage *myImage = [UIImage imageNamed:@"myImageFile.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(menuObject:)];
self.navigationItem.leftBarButtonItem = menuButton;
如果它不起作用,那么試試這個: -
#define setTurqoiseColor [UIColor colorWithRed:68.0f/255.0f green:181.0f/255.0f blue:223.0f/255.0f alpha:1.0]
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:buttonImage style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMenu)];
menuButton.tintColor = setTurqoiseColor;
要設置條形項全局的色調顏色,請在App Delegate中添加以下代碼行
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor redColor]]; // set to your color
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.