Modify UIImage renderingMode from a storyboard/xib file

前端 未结 16 2307
别那么骄傲
别那么骄傲 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:29

    Here's how you can do it in .xib or storyboard files:

    (Obj-C) Create a category on UIImageView:

    @interface UIImageView (Utils)
    
    - (void)setImageRenderingMode:(UIImageRenderingMode)renderMode;
    
    @end
    
    @implementation UIImageView (Utils)
    
    - (void)setImageRenderingMode:(UIImageRenderingMode)renderMode
    {
        NSAssert(self.image, @"Image must be set before setting rendering mode");
        self.image = [self.image imageWithRenderingMode:renderMode];
    }
    
    @end
    

    (Swift 4) Create an extension for UIImageView:

    extension UIImageView {
        func setImageRenderingMode(_ renderMode: UIImage.RenderingMode) {
            assert(image != nil, "Image must be set before setting rendering mode")
            // AlwaysOriginal as an example
            image = image?.withRenderingMode(.alwaysOriginal)
        }
    }
    

    Then in the Identity Inspector in the xib file, add a runtime attribute:

    enter image description here

提交回复
热议问题