0

we are writing an app for iPhones and laying out some graphics, and at one point we are showing a photo. We size it to the screen by using these parameters (in our view controller).

    let view_width : CGFloat = self.view.bounds.width
    let view_height : CGFloat = self.view.bounds.height

On the targets we have used so far, this works fine on an iPhone 7 and iPhone 8. However we just tried it on an iPhone 12 Pro and the image looks stretched, the aspect ratio is off and it is partially off the screen.

Are there parameters we should use other than the above to ensure that we are laying things out according to the screen dimensions, that will work on every phone?

Thanks.

Chris
  • 629
  • 1
  • 10
  • 28
  • see https://stackoverflow.com/questions/185652/how-to-scale-a-uiimageview-proportionally – Mick Dec 22 '20 at 14:06
  • did you try to use aspectFill, it will expand image to use whole screen but it will keep aspect ratio. Second, do you gave it in all 3 sizes 1x, 2x, 3x with appropriate values? – MMDev11070 Dec 23 '20 at 00:01

2 Answers2

0
 let screenSize = UIScreen.main.bounds
    
    let screenWidth = screenSize.width
    
    let screenHeight = screenSize.height
0

In SwiftUI:

Image(...)
        .resizable()
        .scaledToFit()

Will scale the image to fit the screen while respecting it‘s proportions

Schottky
  • 1,549
  • 1
  • 4
  • 19