Size dimensions iOS don't make sense - ios

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.

Related

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.

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.

In swift how do I get the screen size and what is it measured in?

I been trying to or a SKLableNode on top of my screen but if I put the screen size in the y it's off? I noticed the middle of the screen is always the same for every iPhone I use. I also noticed I took the top point of the screen and the bottom point and subtract them and didn't get what the screen height was telling me. Please Help and thank you.
Since you are using SpriteKit I would go in to the GameScene.sks file and make sure the scene orientations are right. Portrait or landscape, whatever you want for your game.
You can also use self.frame.height for y and self.frame.width for x position. If you want to be in the middle of the screen you could just say self.frame.height/2. Screen sizes are measured in pixels and are of type CGFloat. I'm not sure if this was really what you were asking for but it could help you.
i hope this helps you..
var screenwidth : CGFloat!
var screenheight : CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
screenwidth = self.view.frame.size.width
screenheight = self.view.frame.size.height
println("\(screenwidth) ,\(screenheight)")
}

Programmatically align a label from bottom of screen regardless of device - iOS

I've tried
self.lblTimer = [[UILabel alloc] initWithFrame:CGRectMake(x,y,width,height)];
..but since the label i'm looking to position is at the bottom of the screen it goes out of view when i switch devices. I'm trying to make the label 10 pixels in from the left and 10 pixels up from the bottom. It should look consistent regardless if it's viewed on iPhone 4, 5, 6 or 6 plus.
So, your question is about X and Y, right?
The points in iOS start at the top left corner of the screen, so the top left corner of the screen is (0,0).
If you move up/down along the left edge, the X coordinate will continue to be 0. So if you want 10 points to the right of the left edge, X will be 10.
The bottom of the screen can be calculated this way:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
In here, the variable screenHeight will be the bottom line of the screen. So if you want 10 points above the line, you'll need to get (screenHeight - 10).
However, as mentioned by Lyndsey Scott in the comments below, this places the top left corner of your label in the top screenHeight - 10, which could (very likely with a value of 10) place your label out of sight.
To solve this, you also subtract the height of the label so the label is in sight, in the correct spot.
So your final answer is:
CGRectMake(10,(screenHeight - height - 10),width,height);

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