簡體   English   中英

iOS欄項目圖像顯示錯誤的顏色

[英]iOS Bar Item image displaying wrong color

我有一個帶條形圖的條形圖,我的.png圖像有綠色,但當我將它添加到故事板時,它顯示為藍色。

如何讓它顯示圖像?

在此輸入圖像描述

在此輸入圖像描述

使用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]];

你必須設置你的UITabBar tintColor。

在此輸入圖像描述

如果要添加自定義顏色/漸變,可以設置tabBarItem圖像和selectedImage屬性,如下所示:

 customTabBarItem.selectedImage = UIImage(named: "customSelectedImage")!.imageWithRenderingMode(.AlwaysOriginal)
 customTabBarItem.image = UIImage(named: "customUnselectedImage")!.imageWithRenderingMode(.AlwaysOriginal)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM