iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📝

[iOS] Displaying Different Images for Each Screen Orientation

に公開
2020/09/21

What I Wanted to Do

In a ViewController that displays an image in the background, I wanted to achieve the following two things simultaneously:

  1. Display different images for portrait and landscape orientations.
  2. Change the background image based on a certain trigger.

For this explanation, let's use the following example:

  • A normal orange tabby cat appears in portrait mode, and a winking orange tabby cat appears in landscape mode.
  • Pressing a button changes the cat from an orange tabby to a silver tabby.

Approach 1: Doing My Best with a Single UIImageView

Just like layout constraints, a UIImageView allows you to set different images for different screen sizes.
By clicking the + icon to the left of "Image" in the UIImageView inspector, you can set the images to be displayed for each screen size.

Portrait Landscape
vcat hcat

This cleared the first requirement. But what about the second one?
Changing the image must be done in code, but the Outlet for a UIImageView cannot be configured separately for each screen size. This leads to the following problem:

  • If you rotate the screen after changing the image to a silver tabby, the original Storyboard settings are applied, and it reverts to the orange tabby.

I abandoned this method because it didn't seem like this could be easily resolved.

Approach 2: Brute Force Using Multiple UIImageViews

Since the first requirement was met in Approach 1, I tried to achieve the second requirement while incorporating those elements.
Simply increasing the number of UIImageViews and separating the views for the orange tabby and silver tabby would work.
Below is an implementation example for switching the displayed cat:

import UIKit

final class ViewController: UIViewController {
    /// Whether the orange tabby is currently displayed
    private var isTyatora = true
    /// UIImageView to display the orange tabby
    @IBOutlet weak private var tyatoraImageView: UIImageView!
    /// UIImageView to display the silver tabby
    @IBOutlet weak private var sabatoraImageView: UIImageView!
    
    /// UIButton action to switch between the orange tabby and silver tabby
    @IBAction private func catChangeButtonPressed(_ sender: UIButton) {
        self.tyatoraImageView.isHidden = isTyatora
        self.sabatoraImageView.isHidden = !isTyatora
        self.isTyatora = !self.isTyatora
    }
}

This successfully met the requirements.
However, this method has the following disadvantages:

  • Holding an extra UIImageView increases memory usage.
  • It becomes harder to manage as the number of cat types increases (also from a readability perspective).

Since the next method resolved these issues, I will introduce it as my personal best approach.

My Best Approach: Leveraging the Asset Catalog

In Approach 1, we displayed different images for portrait and landscape orientations by setting the image of the UIImageView for each screen size.
However, it turns out we can configure the settings for each screen size directly on the image itself within the Asset Catalog.
The method is the same as with layouts:

  1. For the image you want to differentiate by screen size, change the Height Class from Any to Any & Compact.
    hcasset
  2. Set the image for landscape orientation in the newly appeared Compact Height slot.
    hcasset2

In this case, by applying these settings to both the orange tabby and silver tabby images, you can support portrait and landscape orientations simply by changing the image of a single UIImageView.
gif
Below is an implementation example for switching the image:

import UIKit

final class ViewController: UIViewController {
    /// Whether the orange tabby is currently displayed
    private var isTyatora = true
    /// UIImageView to display the cat
    @IBOutlet weak private var catImageView: UIImageView!
    
    /// UIButton action to switch between the orange tabby and silver tabby
    @IBAction private func catChangeButtonPressed(_ sender: UIButton) {
        let catName = self.isTyatora ? "sabatora_cat" : "tyatora_cat"
        self.catImageView.image = UIImage(named: catName)
        self.isTyatora = !self.isTyatora
    }
}

UIImageView only needs to be one, so even if the number of cat types increases, the number of variables won't increase.
Also, memory usage is lower than in Approach 2.

Conclusion

I often find that the Asset Catalog is very convenient.
By setting colors and images in the Asset Catalog, things become quite convenient in Storyboard... more so than in code.
When I first started iOS development, the projects I participated in wrote all UI in code, so since I started using Storyboard, I've had many eye-opening moments.

As I enter my second year of iOS/Swift, I decided to write this post as a way to start sharing output, even if it's for beginners.
SwiftUI has also evolved significantly, so I'm thinking it's about time I start getting used to it.

Discussion