Jack Schofield
Jack Schofield

Reputation: 302

Objective - C, what does [UIScreen mainScreen].bounds.size.width actually give and what does initWithSize: actually require?

I am writing a GUI, with a main menu, a second screen and a back button to the main menu. From the initial main menu I use the following lines of code:

midScreenX = [UIScreen mainScreen].bounds.size.width/2;
midScreenY = [UIScreen mainScreen].bounds.size.height/2;

    WarScene *battle = [[WarScene alloc] initWithSize: CGSizeMake(midScreenX*2, midScreenY*2)];
    SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
    SKView * skView = (SKView *)self.view;
    UIView *viewToRemove = [self.view viewWithTag:101];
    [viewToRemove removeFromSuperview];
    [skView presentScene:battle transition:reveal];

This works... I think. I open up a new scene, my second scene is the correct size and at least fills the screen. There is a node in that scene which is too big for the screen, and I am working on changing that, but I don't think that that would actually effect the UIScreen's bounds.

The problem arises when I return to the main menu with the following code:

midScreenX = [UIScreen mainScreen].bounds.size.width/2;
midScreenY = [UIScreen mainScreen].bounds.size.height/2;

GameScene *map = [[GameScene alloc] initWithSize: CGSizeMake(midScreenX*2 ,midScreenY*2)];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
SKView * skView = (SKView *)self.view;
UIView *viewToRemove = [self.view viewWithTag:3];
[viewToRemove removeFromSuperview];
[skView presentScene:map transition:reveal];

As far as I can work out, the values being passed through should be exactly the same as the values initially sent for [UIScreen mainScreen].bounds.size and yet the scene which is initialised is far too big.

Is there anything that I could be doing which might affect [UIScreen mainScreen].bounds.size? Or have I misunderstood what init and .bounds actually do?

In terms of what I have already done to try and solve this problem myself, I have looked at examples of how scenes are normally initialised. I see that often values are used in place of .bounds for example :

initWithSize: CGSizeMake(1024,768) 

However, wouldn't that mean that on different devices the scene wouldn't be shown properly/fully?

Upvotes: 1

Views: 1820

Answers (1)

Shebin Koshy
Shebin Koshy

Reputation: 1192

if you are using iOS 6 or iOS 7, both [UIScreen mainScreen].bounds.size.width and [UIScreen mainScreen].bounds.size.height is same in all orientation.. but in iOS 8 it give one value in landscape and give another value in portrait mode

NSLog(@"width %f",[[UIScreen mainScreen] bounds].size.width);  //portrait ios7 or 6 = 320  , landscape ios7 or 6 = 320
NSLog(@"height %f",[[UIScreen mainScreen] bounds].size.height); //portrait ios7 or 6 = 568 ,  landscape ios7 or 6 = 568

NSLog(@"width %f",[[UIScreen mainScreen] bounds].size.width);  //portrait ios8 = 320  , landscape ios8 = 568
NSLog(@"height %f",[[UIScreen mainScreen] bounds].size.height); //portrait ios8 = 568 ,  landscape ios8 = 320

so we can check conditions like this,

CGFloat midScreenX,midScreenY;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
    //landscape mode
    midScreenX = ([[UIScreen mainScreen]bounds].size.width>[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
    midScreenY = ([[UIScreen mainScreen]bounds].size.width<[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
}
else
{
    //portrait mode
    midScreenX = [[UIScreen mainScreen]bounds].size.width/2;
    midScreenY = [[UIScreen mainScreen]bounds].size.height/2;
}

I hope you warScene and gameScene are subclass of SKScene. The method initWithSize returns, A newly initialized scene object. and parameter 'size' is The size of the scene in points.

For more information refer https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKScene_Ref/index.html#//apple_ref/occ/instm/SKScene/initWithSize:

Upvotes: 1

Related Questions