Modify UIImage renderingMode from a storyboard/xib file

前端 未结 16 2188
别那么骄傲
别那么骄傲 2020-12-02 08:00

Is it possible to modify a UIImage\'s renderingMode from a storyboard or xib editor?

The goal is to apply tintColor to the par

相关标签:
16条回答
  • 2020-12-02 08:36

    I got fixed this issue by adding runtime attribute tintColor in interface builder.

    NOTE : You will still need to set your image to be rendered as a template image in your Images.xcassets file.

    0 讨论(0)
  • 2020-12-02 08:37

    As Rudolf also mentioned above, I would define a simple class, like this:

    import UIKit
    
    @IBDesignable class TintImage: UIImageView{
        override func layoutSubviews() {
            super.layoutSubviews()
     
            image = image?.withRenderingMode(.alwaysTemplate)
        }
    }
    

    After this definition, just add an Image View to storyboard and select its custom class as TintImage. This will activate the "Tint" selection in the storyboard.

    0 讨论(0)
  • 2020-12-02 08:38

    It's very easy to fix

    Just create class UIImageViewPDF and use it in your storyboard

    IB_DESIGNABLE
    @interface UIImageViewPDF : UIImageView
    
    @end
    
    @implementation UIImageViewPDF
    
    - (void) didMoveToSuperview
    {
        [super didMoveToSuperview];
        self.image = [self.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        id color = self.tintColor;
        self.tintColor = color;
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-02 08:38

    In iOS 9 setting the tintColor property in Interface Builder is still buggy.

    Note that a working solution besides writing lines directly modifying ImageView properties is to set Render As: Template Image in the asset catalog, and call e.g.:

    [[UIImageView appearanceWhenContainedInInstancesOfClasses:@[[MyView class]]] setTintColor:[UIColor whiteColor]];
    
    0 讨论(0)
提交回复
热议问题