Search code examples
iosswiftscreenlaunchxcode-storyboard

Save image proportion (height) when width of ImageView fill width of parent at LaunchScreen


I want to make Launchcreen.storyboard for my SwiftUI project, and need to resize image for all devices, that width of image = width of screen with saving image proportion. When i fix size on iPhone SE - i have this problem on iPad:

enter image description here

And when i fix height on iPad, i have problem on iPhones:

enter image description here

How can i do it, if i can't change height programmatically in viewDidLoad() (because this is LaunchScreen)?


Solution

  • First, make sure your image content mode is .aspectFit, then you would have to apply the following constraints:

    • Enter the width and height on the image size inspector so you can control + drag from the image to set the aspect ratio
    • Leading and Trailing -> Pinned To Safe Area ( = 0 ) ( so it fits the whole width)
    • Aligned in Center Y ( because you won't always occupy the full height necessarily )
    • Top and Bottom -> Pinned To Safe Area ( >= 0 ) ( so it does not go off the screen)