I am trying to add a background image in my view controller so my sprite kit game scenes can be in the foreground. It's important for me for my background image to stay on screen while the game scenes transition to each other. The problem I am having is how to place the subview I created for my background to be behind my skscene, at the moment my background image is in front of my game scene and all you can see is the image sendSubviewToBack
doesn't seem to work. The following is my viewDidLoad Thanks
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = MainMenuScene(size: CGSize(width: 1536, height: 2048))
let skView = self.view as! SKView
scene.scaleMode = .AspectFill
skView.presentScene(scene)
let backgroundImageView = UIImageView(image: UIImage(named: "bg.jpg"))
backgroundImageView.frame = view.frame
backgroundImageView.contentMode = .ScaleAspectFill
view.addSubview(backgroundImageView)
view.sendSubviewToBack(backgroundImageView)
}
}
What you're describing can't be done with UIKit because your view can't be visible behind the SKView which renders your scene.In iOS 8+ you can use the SKView's
allowsTransparency
property. Documentation states:But in my opinion this is not the best solution. The best solution is to use Sprite Kit, but the problem is you can't preserve nodes across scenes. You must add the node again during each scene transition. This is why I do not use Apple's scene transitions the way the documentation describes. Instead I use my own custom SKNodes and make my own custom transitions using those nodes, which I highly recommend you do. I gave a detailed answer about this here
Generally UI elements are placed on top of each other depending on the order they were added to the view.
If you want to customize that you can use the property
.layer.zPosition
which is any value you pick. The lowest value is in the very back while the highest one is on top.Hope that helps :)
You can use insertSubView: instead of addSubView and specify the index, which determines z-order of views. A view with a higher index lies above those with lower indices.
Make a SKSpriteNode with the background texture!
I dealt with the same issue, and here is how to fix it:
Here is the corrected code:
My particular reason to do this was so I could have a game that would be playable on both iPhones and iPads without aspect ratio distortion. The 'playable area' is always 16:9, and when seen on an iPad, you see a background image that extends to the sides of the playable area.
What I did was to load a background image meant for the iPad (which has an aspect ratio of 4:3), defined the skView to have an aspect ratio of 16:9 (So instead of 1536/2048 I am using 750/1334, for example) and set the scene's scaleMode to .AspectFit. When seen on an iPhone, the scene covers the background completely. When seen on an iPad, you can see the background image complement the sides of the playable area.
Hope this helps!