【问题标题】:tintColor of UIButton's ImageView is not changingUIButton 的 ImageView 的 tintColor 没有改变
【发布时间】:2016-09-06 11:19:02
【问题描述】:

如何更改tintColorbutton.imageView。如果我在UIImageView 中使用与下面相同的渲染模式,tintColor 会发生变化,但按钮的图像视图不会发生同样的情况。是因为 buttonType 吗?

    UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [frontButton setImage:[[UIImage imageNamed:FRONT_IMAGE] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [frontButton.imageView setTintColor:[UIColor redColor]];
    [self addSubview:frontButton];

【问题讨论】:

  • link希望此链接对您有所帮助。

标签: ios objective-c uiimageview uibutton


【解决方案1】:

设置按钮类型“系统”,然后设置色调颜色。 这个解决方案肯定会奏效。请试试这个

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
 [btn setTintColor:YOUR_COLOR];
 [btn setImage:YOUR_Image];

【讨论】:

  • 我可以使用 [btn setBackgroundImage:Your_Image]; ?
  • 两者有区别吗?
  • 好的,有区别。
【解决方案2】:

尝试使用此代码更改 Button 内的色调颜色:

UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:FRONT_IMAGE]];
imageV.image = [imageV.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageV.tintColor = [UIColor redColor];
[frontButton setImage:imageV.image forState:UIControlStateNormal];

【讨论】:

    【解决方案3】:

    你可以做类似的事情,

    UIButton *frontButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [frontButton setImage:[[UIImage imageNamed:FRONT_IMAGE] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [frontButton setTintColor:[UIColor redColor]];
    [self addSubview:frontButton];
    

    给按钮设置色调颜色,如果图像渲染模式是模板,那么它也会得到这个色调颜色。

    如果您想要按钮的不同色调和图像的不同色调,那么您应该使用一个临时图像视图并使用渲染模式模板将图像设置为该图像视图,然后将色调颜色设置为该图像视图,然后将该图像视图中的图像设置为按钮。

    希望这会有所帮助:)

    【讨论】:

      【解决方案4】:

      Swift 4.0

      extension UIImage {
          public static func imageWithRenderingModeAlwaysTemplate(named: String) -> UIImage? {
              let image = UIImage(named: named)?.withRenderingMode(.alwaysTemplate)
              let imageView = UIImageView(image: image)
              return imageView.image
          }
      }
      

      使用

      let button = UIButton.setImage(UIImage.imageWithRenderingModeAlwaysTemplate(named: ""), for: .normal)
      button.tintColor = .red
      

      【讨论】:

        【解决方案5】:

        我找到了答案,我只需要将tintColor 赋予UIButton 而不是按钮的imageView 属性,然后设置好问题中提到的渲染模式。

        [button setTintColor:YOUR_COLOR];
        

        【讨论】:

          【解决方案6】:

          Xamarin iOs 解决方案:

            var image = uiButton.ImageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
            uiButton.SetImage(image, UIControlState.Normal);
            uiButton.TintColor = effect.TintColor.ToUIColor();
          

          【讨论】:

          • 太棒了,我在找这个。问候
          猜你喜欢
          • 2019-12-14
          • 2014-08-31
          • 2016-05-04
          • 1970-01-01
          • 2019-10-07
          • 2017-05-23
          • 2017-05-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多