Modify UIImage renderingMode from a storyboard/xib file

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

    Set tintColor & Class in Storyboard.

    //
    //  TintColoredImageView.swift
    //  TintColoredImageView
    //
    //  Created by Dmitry Utmanov on 14/07/16.
    //  Copyright © 2016 Dmitry Utmanov. All rights reserved.
    //
    
    import UIKit
    
    @IBDesignable class TintColoredImageView: UIImageView {
    
        override var image: UIImage? {
            didSet {
                let _tintColor = self.tintColor
                self.tintColor = nil
                self.tintColor = _tintColor
            }
        }
    
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            initialize()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            initialize()
        }
    
        override init(image: UIImage?) {
            super.init(image: image)
            initialize()
        }
    
        override init(image: UIImage?, highlightedImage: UIImage?) {
            super.init(image: image, highlightedImage: highlightedImage)
            initialize()
        }
    
        func initialize() {
            let _tintColor = self.tintColor
            self.tintColor = nil
            self.tintColor = _tintColor
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 08:16

    You may fix .xib issues with an extension:

    import UIKit
    
    // fixing Bug in XCode
    // http://openradar.appspot.com/18448072
    extension UIImageView {
        override open func awakeFromNib() {
            super.awakeFromNib()
            self.tintColorDidChange()
        }
    }
    

    Source: https://gist.github.com/buechner/3b97000a6570a2bfbc99c005cb010bac

    Amazing, this bug has been around for like 4-5 years now.

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

    Another solution is to create a UIImageView subclass:

    final class TemplateImageView: UIImageView {
        override func awakeFromNib() {
            super.awakeFromNib()
            guard let oldImage = image else { return }
            image = nil
            image = oldImage.withRenderingMode(.alwaysTemplate)
        }
    }
    

    Then just set the class in the Interface Builder to TemplateImageView.

    0 讨论(0)
  • 2020-12-02 08:17
    extension UIImageView {
    
       @IBInspectable var renderModeTemplate : Bool {
           get{
               return image?.renderingMode == .alwaysTemplate
           }
    
           set{
              image = image?.withRenderingMode(newValue ? .alwaysTemplate:.alwaysOriginal)
           }
       }
    }
    
    

    In storyboard select UIImageView and select inspector, set property renderModeTemplate = On In Storyboard

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

    You cann't set renderingMode either from storyboard or xib. It could access by programmatically.

    ex:

    UIImage *unSeletedImage = [UIImage imageNamed:@"UnSelected.png"];
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    0 讨论(0)
  • 2020-12-02 08:24

    You can set the image rendering mode not in the .xib file, but in an .xcassets library.

    After adding an image to an asset library, select the image and open the attributes inspector on the right side of Xcode. Find the attribute 'Render As' and set it to 'template'.

    After setting an image's rendering mode, you can add a tint color to the UIImageView in a .xib or .storyboard file to adjust the image color.

    This sets the property on the image wherever it's used rather than just in one interface builder file, but in almost all cases (that I've encountered) this is the behavior you want.

    Screenshot of Xcode showing attributes inspector for an image

    A few things to note:

    • The image color will not appear to have changed in interface builder (as of Xcode 6.1.1) but will work when the application is run.
    • I've experienced some bugginess with this feature and in some situations I've had to remove and re-add the UIImageView. I have not looked into that deeply.
    • This also works great on other UIKitComponents such as images in UIButton's and UIBarButtonItem's.
    • If you have a bunch of white images that are invisible in your asset library, making them black/transparent images and changing the rendering mode will make your life up to 10x better.
    0 讨论(0)
提交回复
热议问题