Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use template rendering mode in Xcode 6 Interface Builder?

I'm trying to use images with template rendering mode enabled in Interface Builder but I can't get it working. With buttons everything works ok but with ImageViews the tintColor is not applying to the image.

I have enabled "Vectors type" (I'm using pdf) and "Render as Template Image" in the Image assets. What am I doing wrong?

like image 540
rmvz3 Avatar asked Sep 23 '14 14:09

rmvz3


People also ask

What is Xcode template image?

A template image is a bitmap image where only the opacity of the image matters. Typically, you use template images to represent iconic shapes. For example, you might use template images to represent the play and pause buttons of a media app.

What is rendering mode in Swift?

The rendering mode controls how UIKit uses color information to display an image. See Providing images for different appearances for creating tintable images with template mode.

What does rendered image mean on Iphone?

Original rendering mode renders an image as they appear in the original source image. This might be what we expected from most images, such as product images and onboarding illustrations. Example of illustration and article images. Not every image is created equals.

What are Xcode templates?

What are the XCode templates? XCode Templates is a tool for making code snippets to give you a better starting point to accomplish your task with fulfill your needs and speed up the work. In this article, we will be preparing a custom template for MVVM project architecture.

What do you use Xcode for?

With Xcode, every day we create files and groups. As a developer improving work processes is always on their mind. We need tools and solutions to speed up the coding, testing, or organizing of our work. We usually create files for our classes, storyboards, or XIBs. We organize them into folders to have a logically organized project.

How to set combo box options in Xcode?

— Values: It's used for defining the option of the combo box. Now we are all set. Open the Xcode and New->File menu (cmd+N) you should see the below window.


1 Answers

I've hit the same problem. I think this is a bug.

You could dup this radar http://openradar.appspot.com/radar?id=5334033567318016, which refers to this minimal example app https://github.com/algal/TemplateImagesBrokenDemo.

I know of two workarounds for this problem

wrap in UIButton

Since tintColor works for UIButtons, one workaround is instead of UIImageView just to use a custom-type UIButton with userInteractionEnabled=false. If you disable the button's interactivity with UIView.userInteractionEnabled (as opposed to with UIControl.enabled), then you won't change the appearance of the image.

manually re-set the image in code

Another workaround is to re-set the .image property in code, after the UIImageView has been loaded from the nib. This works because setting an image in code seems to be what triggers the templating logic. For this to work, you need to re-set the image to its existing value in a way that won't be optimized away in the compiler. A snippet like this in awakeFromNib has worked for me:

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   } 
like image 93
algal Avatar answered Oct 17 '22 15:10

algal