【问题标题】:How to change tintColor of image in UIButton with imageEdgeInsets?如何使用 imageEdgeInsets 更改 UIButton 中图像的 tintColor?
【发布时间】:2017-05-23 13:42:43
【问题描述】:

我的 Objective-c 应用程序中有一个 UIButton。我的按钮已修改,添加了文本和图像,例如:

- (void)centerButtonAndImageWithSpacing:(CGFloat)spacing {
     CGFloat insetAmount = spacing / 2.0;
     self.imageEdgeInsets = UIEdgeInsetsMake(0, -insetAmount, 0, insetAmount);
     self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
     self.contentEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, insetAmount);
}

然后我用这段代码添加图像:

[self.myButton setImage:[UIImage imageNamed:@"imageButton.png"] forState:UIControlStateNormal];

我想更改图像的 tintColor,但是当我尝试更改时,我的代码不起作用:

[self.myButton setTintColor:[UIColor redColor]];

我正在尝试使用此代码,但没有成功:

UIImage* img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = [UIColor redColor];

我有这个:

我想得到它:

如何更改 imageButton 的颜色?

【问题讨论】:

标签: ios objective-c uibutton tintcolor uiedgeinsets


【解决方案1】:

我们可以通过以下方式为透明图标应用所需的颜色。

1.将图像拖到Assets.xcassets 并在资产属性检查器中将Render as 属性更改为Template Image

2.并像下面这样设置所需的色调。

icon.tintColor = [UIColor redColor];

希望对你有帮助。

【讨论】:

    【解决方案2】:

    改变 tintColor 属性的解决方案是 setImage 和 renderingMode:

     imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
    

    使用此代码:

    [self.myButton setImage:[[UIImage imageNamed:@"myImageButton.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    

    然后使用此代码将图像设置为按钮,我们可以正确设置 tintColor 属性:

     self.myButton.tintColor = [UIColor redColor];
    

    【讨论】:

      【解决方案3】:

      在 Interface Builder 中或以编程方式将 UIButtonType 更改为 UIButtonTypeCustom

       UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      

      自行更改属性并重新创建圆角

      myButton.backgroundColor = [UIColor redColor];
      myButton.layer.borderColor = [UIColor blackColor].CGColor;
      myButton.layer.borderWidth = 0.5f;
      myButton.layer.cornerRadius = 10.0f;
      

      让我知道状态..

      【讨论】:

      • 如果我的按钮是添加到情节提要的 IBOutlet,我该如何应用它?
      • @user3745888 当您在情节提要中选择按钮时,您将看到类型,只需将其更改为自定义
      • 我已经添加了这个,但是这个改变了背景,这个不改变uiimage的颜色..
      【解决方案4】:

      像这样在按钮中的 ImageView 上创建一个图层并设置所需的颜色和蒙版。

      for (UIView *view in myButton.subviews) {
              if ([view isKindOfClass:[UIImageView class]]) {
                  CALayer *mask = [CALayer layer];
                  mask.backgroundColor = [UIColor redColor].CGColor;
                  view.layer.mask = mask;
              }
          }
      

      【讨论】:

        【解决方案5】:

        Swift 4 和 4.2

        let img = UIImage.init(named: "buttonName")?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
                    btn.setImage(img, for: .normal)
                    btn.tintColor = .gray
        
        }
        

        【讨论】:

          【解决方案6】:

          斯威夫特 5

          let examButton = UIButton(image: UIImage(systemName: "rectangle.dock")!, tintColor: .systemRed, target: self, action: #selector(showExamController))
          
          examButton.setTitleColor(.label, for: .normal)
          examButton.setTitle("   12K", for: .normal)
          

          【讨论】:

            猜你喜欢
            • 2015-04-10
            • 2011-01-27
            • 1970-01-01
            • 2019-12-04
            • 2011-06-01
            • 2013-10-16
            • 1970-01-01
            • 2015-11-22
            • 2015-01-06
            相关资源
            最近更新 更多