Screen hight with (UIScreen) is fine in iPad but wrong in iPhone - ios

I am developing a game where a background is moving at the back. I sat the background hight using the below code. It is working fine with iPads but with iPhones it is not taking the proper hight of the screen; instead it is in the middle of the screen with almost half the hight of the screen! The application is only working in portrait orientation. This is for xCode 8 & SWIFT 3
Please check below the code I am using for the background
hight:
let screenSize: CGRect = UIScreen.main.fixedCoordinateSpace.bounds
background1.size.height = screenSize.height
Thanks in advance

I have resolved the issue but using a (workaround) rather than resolving the root cause as I could not identify it.
First I have noticed that when sizing the hight of the screen I have 2 conditions:
below code will work with all iPhones but not iPads:
background1.size.height = self.frame.height
below code will work with all iPads but not iPhones:
background1.size.height = screenSize.height
So since the minimum hight of screen of iPads is 1024; I used the below code to first check if it is iPad use condition 2 and if iPhone use condition 1 as follow:
let screenSize: CGRect = UIScreen.main.bounds
if screenSize.height < CGFloat(1024) {
background1.size.height = self.frame.height ////for iphone
} else {
background1.size.height = screenSize.height ////for ipad
}

I am not sure what your case is.But it happens in iPhone 5+ devices due to launch screen size.Normally it behaves like iPhone 5 screen size even with iPhone 6,iPhone 6+.
Now the soultion.
1.You can use the default launch screen storyboard to avoid the problem.
2.You have to add every sizes for iPhone screen size (320*480,640*960,640*1136,768*1334,1242*2208).please add also iPhone 7,7+ sizes too.
3.You can use asset Catalogs for launch screen.
If thats the case for you please use above solution.It should work.

Related

Device orientation not detecting in Today widget for iPhone device 4s,5,5s and 6

I am using below code to check orientation of device in today widget code base
but when I change mode to landscape for device iPhone 4s, 5, 5s and 6 then below code won't work.
let orientationSize: CGSize = UIScreen.mainScreen().bounds.size
if (orientationSize.width / orientationSize.height > 1) {
numberOfEvents = isiPad ? ipadLandscapeModeNumberOfEvents : iphoneLandscapeModeNumberOfEvents
}
I have tried below methods and all are won't work
override fun viewWillTransitionToSize
override fun willRotateToInterfaceOrientation
I am stuck in this long time.
Please note that issue is only for iPhone 4s,5,5s and 6.
You are looking at the bounds (size) before the orientation change within:
willRotateToInterfaceOrientation
so that will show you current size before orientation actually changes. I suggest you to check the size with
CGSize = UIScreen.mainScreen().bounds.size
within following method:
didRotateFromInterfaceOrientation

UIButton does not position itself correctly and not showing up in some devices - Objective C

I'm trying to align in center two buttons in a single-view application. I don't want to use auto-layout, i try to solve this case programmatically. So in my ViewController.m, in the ViewDidLoad() function, i've put the following code:
_firstButton.center = CGPointMake(self.view.center.x, self.view.frame.size.height/2.0-235);
_secondButton.center = CGPointMake(self.view.center.x, _secondButton.frame.size.height/2.0+(self.view.frame.size.height*0.9));
The secondButton is correctly positioned in any screen, on any device and on any simulator. The firstButton is positioned right only in iphone 5 and 5S simulators, but it does not even show up in my iPhone 5C device or the iPhone 6 & 6 Plus simulators.
What am i doing wrong, what do i miss here?
Thanks in advance
ps: objective-c noob here, sorry if that's a too dumb question
In viewDidLoad the frame is not set yet. I recommend you to do it in viewWillAppear
Be sure it is NOT a dumb question.
As a little help, I can tell you I find easier to handles layout with main xib with auto layout.
Otherwise, for multiple screens sizes, you could manage sizes or positions as relatives to actual screen size : UIScreen.mainScreen().bounds, for instance, in case of sizes :
let screenSize: CGRect = UIScreen.mainScreen().bounds
let sreenHeight = screenSize.height
let screenWidth = screenSize.width
let cardwidth = screenSize.width * 0.47
let cardheight = screenSize.height * 0.47
let ViewBackGround = UIView(frame:CGRectMake(0, 0, cardwidth * 0.8, cardwidth))

Implementing iOS graphics in Xcode?

I've started to create an iPhone game. I have created a background in a design application and set the initial size to the iPhone 5 dimensions. I have imported the image as #2x The code I have implemented is the following,
let backGround = SKSpriteNode(imageNamed: "Background")
backGround.position = CGPointZero
backGround.anchorPoint = CGPointZero
backGround.blendMode = SKBlendMode.Replace
self.size = CGSizeMake(backGround.size.width, backGround.size.height)
self.addChild(backGround)
Running in the simulator, the background displays in the correct size for the iPhone 5/5s and the iPhone 6/6 Plus. However, with the iPhone 4s and all iPad devices, The top half and bottom half of my image are cut off. How do i go about fixing this ?

Trying to detect iPhone 6 Plus in landscape orientation

I have a today widget for my app and I have been setting the layout of objects in code (Not using auto-layout) however, I need to detect when a user is using an iPhone 6 Plus. The reason for this is because in notification centre on the 6 Plus, there is a border around the edge which cuts off the labels in my widget (in landscape).
The problem is that I have been having issues trying to figure out what height value the screen is in landscape.
This is the code I have right now:
var height = UIScreen.mainScreen().bounds.size.height
if UIDevice.currentDevice().orientation.isLandscape && height == 414 {
totalBudgetDisplay.frame.origin.x = UIScreen.mainScreen().bounds.width - 250
currentBudgetDisplay.frame.origin.x = UIScreen.mainScreen().bounds.width - 250
budgetNameDisplay.frame.origin.x = 80
warningLabel.center = view.center
button.frame = CGRectMake(0, 0, view.bounds.width, view.bounds.height)
}
Is this the correct value for the screen height in landscape? If so, why are the labels still not moving?
Your code is good but UIDevice.currentDevice().orientation.isLandscape just does not get you correct value.
Seems like there is no bullet-proof solution to detect device orientation for Extensions.
Look at this answer for possible workaround - https://stackoverflow.com/a/26023538/2797141

xcode 6 is iphone 5 no longer 568 points in height?

So I'm trying to update my game to fit the iPhone 6 properly and i see now that when i run my game on the iPhone 5 simulator, everything is fitting as if it were an iPhone 4. I use if statements to determine the height of the device:
if ((int)[[UIScreen mainScreen] bounds].size.height == 568)
{
mylabel4.fontSize = 35;
mylabel4.position = CGPointMake(CGRectGetMidX(self.frame) -145, CGRectGetMidY(self.frame) -130);
} else {
mylabel4.fontSize = 35;
mylabel4.position = CGPointMake(CGRectGetMidX(self.frame) -110, CGRectGetMidY(self.frame) - 130);
}
And like i said when i run the iPhone 5 simulator, it runs the code in the 'else' bracket. Its like the iPhone 5 is no longer 568 points in height. This was working fine until i download Xcode 6? What's going on here?
The UIScreen bounds behavior has changed in iOS8. Previously it would always give you width and height of the screen in portrait (so 320 x 568 on the iPhone 5), but in iOS8 it will give you width and height with respect to whatever orientation you're in, i.e. in landscape you would get 568 x 320.
If you want to get the bounds in portrait (like before), use:
[UIScreen mainScreen].fixedCoordinateSpace.bounds.size
Just add launch images for the iPhone and the simulator will display the proper points. Weird bug.

Resources