Search code examples
iosuiimageviewuiaccessibilitysf-symbolsdynamic-type-feature

UIImageView intrinsic content size behavior with Dynamic Type


I have an image view with its image set with a custom image (svg file with Preserve Vector Data selected). The image view has no constraint and it is told to adjust image size with accessibility.

When I run the app and change the Dynamic Type of the app in the Environment Overrides button in the Xcode Debug Area, I see the following behavior :

  1. For Dynamic Types that are below Accessibility 1 (not included) : the image view remains with a size of 24x24 (its intrinsic content size).
  2. For Dynamic Types that are above Accessibility 1 (included) : the image view gets bigger at each Dynamic Type increment.

What is weird is that if I use a SFSymbol instead of a custom image in the image view's image, the behavior for 1. is not the same : the image view has a different intrinsic content size for each Dynamic Type increment (wether it is above or below Accessibility 1).

- Why is that ?

- How can make my custom image view behave like the one with the SFSymbol ?


Solution

  • Why is that ?

    The Dynamic Type feature automatically handles the image size only when dealing with accessibility. This point is highlighted through the Xcode Interface Builder for instance: 🤓 enter image description here

    How can make my custom image view behave like the one with the SFSymbol ?

    As stated in your question, your 'image view has no constraint and it is told to adjust image size with accessibility.'
    If you're willing to manage this adjustement for all the possible sizes (including those lower than the a11y's), you should take a look at the following steps:

    • First, be informed of a Dynamic Type size modification through the traitCollectionDidChange (see the notification section).
    • Then, take into account only the sizes that aren't a11y with the preferredContentSizeCategory method.
    • Finally, automatically adapt the image size with an appropriate reference through the scaledValue method.

    And for the record, you may also limit the minimum and maximum sizes since iOS 15 if need be. 😉

    I suggest to take a look at this Keith Harrison's article that covers both UIKit and SwiftUI (easiest implementation) for an example that can be easily adapted to your image size. 👍