LukeTerzich
LukeTerzich

Reputation: 554

Swift Frames are all out of alignment

I am creating a game, when I navigate the menu scene to my settings scene (using a button) it all works fine.

But when i go to position say a UISlider on my settings scene the positioning is all out. By this i mean if i try to place it by the self.frame.size.width/2 it is about 3/4 the way in on the screen and nowhere near the middle...

I'm sure its something wrong with the way I am progressing from scene to scene - This is my code for that

func settingsScene() {

    println("Settings Scene Pressed!")
    var settingscene: Settings = Settings(size: CGSizeMake(768, 1024))
    var spriteView: SKView = self.view as SKView!
    settingscene.scaleMode = .AspectFill
    var trans :SKTransition = SKTransition.doorsOpenHorizontalWithDuration(0.5)
    spriteView.presentScene(settingscene, transition: trans)
    settingsButton.removeFromSuperview()
    button.removeFromSuperview()

}

The above code is on my menu scene and runs when I press my "Go to settings button"

I'm sure the problem is with the size: CGSizeMake(768,1024) line

This is my code for the slider - I am not sure how to position is in the middle of the scene

    sliderDemo.frame = CGRectMake(0.0, 0.0, 100.0, 50.0)
    sliderDemo.frame.origin = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    sliderDemo.minimumValue = 0.5
    sliderDemo.maximumValue = 5.0
    sliderDemo.continuous = true
    sliderDemo.tintColor = UIColor.redColor()
    sliderDemo.value = 3.0
    sliderDemo.addTarget(self, action: "sliderValueDidChange:", forControlEvents: .ValueChanged)
    view.addSubview(sliderDemo)

Upvotes: 0

Views: 114

Answers (1)

Christian
Christian

Reputation: 22343

First I would recommend, that you don't use hardcoded sizes when initializing your SettingsScene. I would recommend that you use the size of your current scene. Something like that:

var settingscene: Settings = Settings(size: self.size)

So the different scene are always the same size.

But to solve your problem. The problem here is, that the GameViewController and the GameScene haven't the same size by default. So you have to change the sizes to match each other:

To do that, open your GameViewController and add the following line in your viewDidLoad-method after the line scene.scaleMode = .AspectFill, and before skView.presentScene(scene):

    scene.size = skView.bounds.size

That will set the size of your scene correctly.

Upvotes: 1

Related Questions