2

We have 3 targets for 3 different apps offering similar sets of features but different UI. all these 3 targets are having different .xcassets files. But now as per Apple 4.3 guideline, they are asking us to create one container and handle different UI from the same container.

Now as we already have different image assets files for different targets, how can I programmatically switch between different .xcassets files.

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Praful Kadam
  • 372
  • 6
  • 22

2 Answers2

0

You can have multiple xcassets for multiple targets, let's say you have A, B, C target and have three different xcassets for each target, you can solve this issue with two ways:

  1. Add images with the same name for different targets and access the images directly. Xcode will run time access specific xcassets images.

  2. Add checks for different targets at the time of accessing images

Coding Example:

if target == "A" {
   let imageView = UIImageView(image: UIImage(named: "firstImage"))
}
if target == "B" {
  let imageView = UIImageView(image: UIImage(named: "secondImage"))
}
if target == "C" {
  let imageView = UIImageView(image: UIImage(named: "thirdImage"))
}
Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37
0

First I have created multiple image version is same xcasset file and then I created an extension for UIImage and wrote convenience initializer where I check the current target and assign image name accordingly

extension UIImage {

convenience init?(editableNamed: String) {
    let newName = appDel.target != "target1" ? editableNamed : "\(editableNamed)_target1"
    if (UIImage(named: newName) != nil) {
        self.init(named: newName)
    }
    else {
        self.init(named: editableNamed)
    }
}

So whenever I assign image like

cell.imgCheck.image = UIImage(editableNamed: "iconCheck")

It checks the current target and then assigns appropriate image from xcasset file.

Praful Kadam
  • 372
  • 6
  • 22