Why does an iPhone 6's frame return 1024 for width?

Discussion in 'iOS Programming' started by chrono1081, Jun 26, 2015.

  1. chrono1081 macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #1
    Hi guys,

    I'm trying to work in Sprite Kit and am finding the screen sizing incredibly frustrating. If I start with a blank scene, the self.frame.size.width returns 1024 on iPhone 6, not the expected 1334 for pixels, or 667 in points (points being another mess of confusion).

    Nowhere in the code is there a spot that sets the frame size to 1024, where does this value come from? Is querying the frame not the right way to get a screens size?

    I know I can manually set it correctly in the viewWillLayoutSubviews method but I'm trying to figure out where it comes from. Any help is greatly appreciated. And yes, I've read through Apples sprite kit programming guide which isn't helpful with regards to creating graphics or managing screen sizes.
     
  2. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    Make sure to wait for the app to finish laying out all its visible views. If you ask before (auto-)layout is finished, you can get bogus sizes (xib or storyboard defaults unrelated to the device).
     
  3. chrono1081, Jul 14, 2015
    Last edited: Jul 14, 2015

    chrono1081 thread starter macrumors 604

    chrono1081

    Joined:
    Jan 26, 2008
    Location:
    Isla Nublar
    #3

    Thank you. How would I go about doing that? I see people say don't use viewDidLoad and instead use viewWillLayoutSubviews but this doesn't appear to work in iOS 8. Nothing appears at all if I move the view configuration into viewWillLayoutSubviews.

    Any idea if there is some different way?

    EDIT: I found a way, I can do this: (The scene.size = skView.bounds.size line is the key).

    Code:
    - (void)viewDidLoad
    
    {
    
        [superviewDidLoad];
    
        // Configure the view.
    
        SKView * skView = (SKView *)self.view;
    
        skView.showsFPS = YES;
    
        skView.showsNodeCount = YES;
    
        /* Sprite Kit applies additional optimizations to improve rendering performance */
    
        skView.ignoresSiblingOrder = YES;
    
    
        // Create and configure the scene.
    
        GameScene *scene = [GameSceneunarchiveFromFile:@"GameScene"];
    
        scene.size = skView.bounds.size; //This is the key!
    
        scene.scaleMode = SKSceneScaleModeAspectFill;
    
    
        // Present the scene.
    
        [skView presentScene:scene];
    
    }
    
     

Share This Page