【问题标题】:iOS Bar Item image displaying wrong coloriOS 栏项目图像显示错误的颜色
【发布时间】:2015-12-20 16:08:54
【问题描述】:

我有一个带有 Bar Items 的栏,我的 .png 图像是绿色的,但是当我将它添加到情节提要时,它显示为蓝色。

如何让它按原样显示图像?

【问题讨论】:

  • 尝试设置tintColor 有时它可以帮助我设置图像的实际颜色。

标签: ios swift storyboard


【解决方案1】:

使用 UIBarButton 的tintColor 为图像设置所需的颜色。 如果绝对需要使用原始图像颜色,请使用它来设置图像:

[aBarButton setImage:[[UIImage imageNamed:@"xyz.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

【讨论】:

    【解决方案2】:

    The docs对此有点模棱两可

    栏上显示的图像来自该图像。如果这 图像太大而无法放在栏上,它被缩放以适合。通常, 工具栏和导航栏图像的大小为 20 x 20 点。这 源图像中的 alpha 值用于创建图像——不透明 值被忽略。

    基本上这是说您提供的图像不会是实际显示的图像。相反,系统使用图像的 alpha 蒙版和项目的 tintColor 来生成最终显示。

    【讨论】:

    • 重新阅读我的答案,它与项目的tintColor 结合使用,尝试将其设置为绿色。如果您想要不止一种纯色,则需要使用其他方法(请参阅Ishan's answer
    【解决方案3】:

    以编程方式添加图像

    [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;
    

    【讨论】:

      【解决方案4】:

      要全局设置条形项目的色调颜色,请在您的 App Delegate 中添加这些代码行

      UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
      [barButtonAppearance setTintColor:[UIColor redColor]]; // set to your color
      [[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
      

      【讨论】:

        【解决方案5】:

        您必须设置您的 UITabBar tintColor。

        如果您想添加自定义颜色/渐变,可以如下设置 tabBarItem 图像和 selectedImage 属性:

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

        【讨论】:

          猜你喜欢
          • 2015-09-15
          • 2015-07-13
          • 1970-01-01
          • 2018-07-15
          • 2014-10-10
          • 1970-01-01
          • 2017-02-27
          • 2015-07-14
          • 1970-01-01
          相关资源
          最近更新 更多