4

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?

1
  • 1
    don't present scene in viewDidLoad, move it to viewWillLayoutSubviews to get correct size. Look around to find the proper way to do this.
    – CodeSmile
    Feb 2, 2014 at 10:08

2 Answers 2

4

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

1
  • What results are you getting?
    – bneely
    Feb 4, 2014 at 20:30
0

[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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.