如何在Xcode 6 Interface Builder中使用模板渲染模式?

rmv*_*vz3 26 interface-builder uiimageview ios

我正在尝试使用在Interface Builder中启用模板渲染模式的图像,但我无法使其正常工作.使用按钮一切正常,但使用ImageViews,tintColor不适用于图像.

我在Image资源中启用了"向量类型"(我正在使用pdf)和"渲染为模板图像".我究竟做错了什么?

alg*_*gal 34

我遇到了同样的问题.我认为这是一个错误.

您可以复制这个雷达http://openradar.appspot.com/radar?id=5334033567318016,这是指这个最小的示例应用程序https://github.com/algal/TemplateImagesBrokenDemo.

我知道这个问题的两个解决方法

换行于UIButton

由于tintColor适用于UIButtons,因此一种解决方法是使用自定义类型的UIButton而不是UIImageView而使用userInteractionEnabled = false.如果禁用按钮与UIView.userInteractionEnabled的交互性(与UIControl.enabled相反),则不会更改图像的外观.

手动在代码中重新设置图像

另一种解决方法是.image在从nib加载UIImageView之后,在代码中重新设置属性.这是有效的,因为在代码中设置图像似乎是触发模板逻辑的原因.为此,您需要以不会在编译器中优化的方式将图像重新设置为其现有值.像这样的片段awakeFromNib对我有用:

override func awakeFromNib() {
  super.awakeFromNib()

  if shouldSetImagesManually {
    // the following three-lines should in theory have no effect.
    // but in fact, they ensure that the UIImageView
    // correctly applies its tintColor to the vector template image

    let image = self.templateImageView.image
    self.templateImageView.image = nil
    self.templateImageView.image = image
  }
Run Code Online (Sandbox Code Playgroud)

  • 我发现在UIImageView上调用`tintColorDidChange`就足够了. (8认同)
  • Jaroslav解决方案可用于通过UIImageView的扩展来解决问题:https://gist.github.com/buechner/3b97000a6570a2bfbc99c005cb010bac (2认同)
  • iOS 11仍然没有修复. (2认同)

Dmi*_*rev 11

在我的情况下,如果应用程序是使用iOS8 SDK构建并在iOS 7上运行,则会出现问题.

我的解决方法是:

// this is the code that *should* work and does so on iOS 8
UIColor *tintColor = [UIColor colorWithWhite:1.0 alpha:0.3];
[self.iconImageView setTintColor:tintColor];
self.iconImageView.image = [self imageForIconImageView]; // image is loaded from a pdf-resource (asset catalog set as Template) with imageNamed:@"resourceName"

// this is the workaround to get tint on iOS 7    
if (!IS_IOS8_OR_HIGHER) { // macros checking iOS version*

    UIImage *templateImage = [self.iconImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.iconImageView.image = templateImage;
}

// * - macros is defined like so:
// #define IS_IOS8_OR_HIGHER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
Run Code Online (Sandbox Code Playgroud)


Zol*_*tán 5

这是我发现的最简单的无代码解决方案:

在运行时属性中设置tintColor