'Find screen resolution in Swift [duplicate]
I am writing an iOS game in Swift using Spritekit and want to find the screen resolution to properly place my sprites. I found multiple ways on internet, but none of them is giving me a correct resolution that help me to place my sprites. The one that works fine for an iPhone 13 Pro is the following
screenSize=self.size
screenSize.width /= (UIScreen.main.bounds.height/screenSize.height) / (UIScreen.main.bounds.width/screenSize.width)
let background = SKSpriteNode(imageNamed: "Landscape.jpg")
background.position = CGPoint(x: 0, y: 0)
background.size = screenSize
If I use the recommended UIScreen.main.bounds, this is the outcome on an iPhone:
But on an iPad for example, the dimensions are too big.
Is there a unique way of finding screen resolution on all devices ? Or is there a scene scaling that enters in the equation ?
Solution 1:[1]
Try this:
// Get main screen bounds
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
print("Screen width = \(screenWidth), screen height = \(screenHeight)")
this is output of iPone 13 pro simulator:
Screen width = 390.0, screen height = 844.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Fabio |
