Find screen resolution in Swift [duplicate] - ios

This question already has an answer here:
Swift - SpriteKit CGPoint Alignment
(1 answer)
Closed 9 months ago.
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 ?

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

Related

Frame of CardIO view

I am working on an iOS app in Swift 3.0 and I have integrated card scanner using Card.IO for iOS.
I am successfully able to scan the card, but the problem is that width and height of the camera view does not take frames as per the requirements.
It only takes width and height in ratio of 3:4.
I want, the camera should take half of the screen height and full screen width, But is is not taking. When I pass the frame as
cardView = CardIOView(frame: CGRect(x: 0, y: 100, width: screen.width, height: screen.height / 2))
it does not take full screen width.
Is it a bug on SDK side, I have tried everything but no success.
If anyone can help.
Thanks in advance!
My suggestion is: you incorrectly determine the screen size: Swift: Determine iOS Screen size
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height

Horizontal UIScrollView Not Fitting iPhone 5 Screen

I'm using a UIScrollView to swipe horizontally between two ViewControllers. On iPhone 6 and higher the VCs fit perfectly in the screen. However, when testing on an iPhone 5, the second VC isn't displayed entirely. The scrollView will not display all of "Page 2" (the right VC), but will fit the left VC perfectly. I have the scrollView's contentSize width set to equal 2 times the screen's width (since there are two full screen VCs).
let screenRect = UIScreen.main.bounds
var adminFrame : CGRect = pageTwo.view.frame;
adminFrame.origin.x = adminFrame.width;
pageTwo.view.frame = adminFrame;
var BFrame : CGRect = pageOne.view.frame;
BFrame.origin.x = 2 * screenRect.width;
self.scrollView.contentOffset.x = BFrame.width
self.scrollView.contentSize = CGSize(width: screenRect.width * 2, height: screenRect.height)
If I arbitrarily add to the contentSize width, then it does fit better, but is not a perfect fit nor does it seem like a good solution.

Size dimensions iOS don't make sense

I'm trying to look up the bounds of my screen in Portrait mode (set to only Portrait in Xcode) using Swift but everything I try does not place my sprite on a sensible position.
let screenWidth = self.size.width;
let screenHeight = self.size.height;
var posX: CGFloat = screenWidth;
var posY: CGFloat = screenHeight / 2;
Sprite is positioned out of screen in portrait. Turning the screen reveals the sprite far right of the screen (width), in the middle (height).
So you'd think I should just turn around these values.
let screenWidth = self.size.height;
let screenHeight = self.size.width;
Nope. Still out of screen in portrait. Turning the screen again reveals it, but halfway between middle and border of the right screen border (width), and not in the middle (height).
So I guess I could try to get the values in some other way.
let screenWidth = UIScreen.mainScreen().bounds.width;
let screenHeight = UIScreen.mainScreen().bounds.height;
It's visible in portrait!... But on the far left (not on the border) and not in the middle of the screen's height either. Turning the screen places it a bit left of the middle, and still not center of height.
I think you can guess what I tried next.
let screenWidth = UIScreen.mainScreen().bounds.height;
let screenHeight = UIScreen.mainScreen().bounds.width;
Pretty far on the bottom, slighty right of the middle in portrait. Turning screen makes no sense either.
I'm kinda lost. Googled and tried things for hours but no result.

How do I scale my imagePickerController to fit the screen?

I've created an imagePickerController which I am trying to create a custom overlayView for. I have done this, but when hiding the camera controls, I was left with a big black space.
So I tried to fill this by attempting to dynamically scale my camera to fit the screen, no matter what device is used. The result is that on my iphone 6, the camera does now fill the screen but it is super zoomed in and I don't know how to counteract this. Help much appreciated. This is my code:
let screenBound = UIScreen.mainScreen().bounds.size
let cameraAR = 4.0/3.0 as CGFloat
let cameraVH = screenBound.width * cameraAR
let scale = screenBound.height / cameraAR
imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0, screenBound.height - cameraVH / 2.0)
imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, scale, scale)
You cannot do this in default imagePickerController. You have to 'build' your own, unfortunately.

Dynamically centering UIImagepicker viewfinder that has been scaled up to full screen

I'm making a full screen camera for the iPhone 5 and have the following code to scale the 4:3 camera to fill the entire screen, which is a 2:3 ratio. The left and right sides bleed off the screen.
I have to move the cameraView down 71 points in order for it to center with the screen. Otherwise, there's a black bar at the bottom. I'm not quite sure why. Because I don't know why this is happening, I can't figure out how to dynamically code the adjustment to accommodate the iPhone 6 and 6 Plus.
Any help is appreciated.
// get the screen size
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// establish the height to width ratio of the camera
float heightRatio = 4.0f / 3.0f;
// calculate the height of the camera based on the screen width
float cameraHeight = screenSize.width * heightRatio;
// calculate the ratio that the camera height needs to be scaled by
float ratio = screenSize.height / cameraHeight;
//This slots the preview exactly in the middle of the screen by moving it down 71 points (for iphone 5)
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0);
self.camera.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, ratio, ratio);
self.camera.cameraViewTransform = scale;
This finally clicked in my head. Since we know that the camera will always be the same length of the screen width:
//getting the camera height by the 4:3 ratio
int cameraViewHeight = SCREEN_WIDTH * 1.333;
int adjustedYPosition = (SCREEN_HEIGHT - cameraViewHeight) / 2;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
self.imagePicker.cameraViewTransform = translate;

Resources