简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM