Dmitry Klochkov Dmitry Klochkov - 1 month ago 17
iOS Question

Create SKView programmatically?

I like my code to be explicit so I want to remove the storyboard file and create SKView programmatically.

What I have tried so far:


  1. I created a SpriteKit game project in xcode.

  2. Deleted the storyboard file.

  3. Deleted sroryboard property from
    Info.plist

  4. Added this code found in an example to the
    AppDelegate
    :

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    AppViewController *viewController = [[AppViewController alloc] init];
    viewController.edgesForExtendedLayout = UIRectEdgeNone;
    _window.rootViewController = viewController;
    [_window makeKeyAndVisible];
    return YES;


    }

  5. Added this code to the
    AppViewController
    :

    -(void) loadView
    {
    self.view = [[SKView alloc] init];
    }



The app starts but then crashes on the creation of first sprite with the following warning


EXC_BAD_ACCESS


I think there should be some good examples of how to do this, but I have not found any for the Sprite Kit yet.

Answer

I fixed the problem by setting a frame to the view:

- (void) loadView
{
    self.view = [[SKView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
}

Here is a working example project in Swift