Asset Catalogs have been around for a few Xcode releases. They’re a great way for us to organize and configure graphical assets (among many other things) for our app. Today we’ll check out the improvements to Asset Catalogs in Xcode 9. Let’s dive in.
First up, colors.
Yep, we can now define and organize named colors inside of an Asset Catalog!
We can select “New Color Set”, and then use all of the normal features of Asset Catalogs, including new Wide Gamut support.
Then, in our code we can reference these colors like this:
view.backgroundColor = UIColor(named: "wood")
The second big addition we’re going to look at is “real” vector-based asset support.
In past Xcode releases, we were able to add image assets to our catalogs with a format of PDF. This worked great, but under the hood, Xcode would simply render our asset at the
@3x sizes and save non-vector (i.e. png images) into our app’s bundle.
In Xcode 9 however, we’re given a beautiful new checkbox called “Preserve Vector Data”.
This means that if our image is loaded in our code, and we ask it to display at a a larger size than it is by default, it will be scaled up at runtime by the system.
This means we can now write code like this without any quality loss at render time:
let normal = UIImageView(image: UIImage(named: "cocoapod")) normal.tintColor = UIColor(named: "covfefe") view.addSubview(normal) let big = UIImageView(image: UIImage(named: "cocoapod")) big.tintColor = UIColor(named: "cream") big.frame = CGRect(x: 50, y: 200, width: normal.bounds.size.width * 2, height: normal.bounds.size.height * 2) view.addSubview(big)
Very, very cool.