tingxuezisu
tingxuezisu

Reputation: 41

IOS: Cannot get correct screen size

I tried to display some SKNodes and I used the width and height of screen to set their positions in order to make them compatible with different screen sizes.

SKNode* obstacle = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:obstacleSize];
obstacle.position = CGPointMake(80, self.size.height * 0.38);
obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.frame.size];

But nodes' positions still change when I run the program with a different screen size (fine in 3.5 inch but wrong in 4-inch). And I found that self.size.height is not the actual height of the screen.

Does anyone know how to solve this problem?

Upvotes: 4

Views: 3096

Answers (2)

Josh Nussbaum
Josh Nussbaum

Reputation: 33

[UIScreen mainScreen].bounds.size should return the correct CGSize vector of the width and height. However, if you are in landscape, the values will be reversed, then what'd you would expect from portrait.

Upvotes: 0

bneely
bneely

Reputation: 9093

[UIScreen mainScreen].bounds.size returns a CGSize with the width and height of the screen. See the UIScreen Apple documentation.

Upvotes: 4

Related Questions