I have a SwiftUI View with a VStack of 3 SpriteKit SKScenes inside. While scoreView and accelerometerView have a fixed height, gameScene does not. It takes the remaining space and depends somehow on the device.
struct ContentView: View {
@StateObject private var gameScene = GameSKScene()
@StateObject private var scoreView = ScoreSKScene()
@StateObject private var accelerometerView = AccelerometerSKScene()
var body: some View {
VStack {
SpriteView(scene: scoreView)
.frame(height: 50)
SpriteView(scene: gameScene)
SpriteView(scene: accelerometerView)
.frame(height: 50)
}.ignoresSafeArea()
}
}
Now inside gameScene have problems setting up the scene because the scene itself has no information of its size.
I can get the width with UIScreen.main.bounds.width
but how do I get the actual height?
If I understand you correctly, you want the gameScene
SpriteView
to fill the remaining space after accounting for the other two scenes. You can use GeometryReader
(documentation here).
Example usage:
var body: some View {
GeometryReader { geo in
VStack {
SpriteView(scene: scoreView)
.frame(height: 50)
SpriteView(scene: gameScene)
.frame(height: geo.size.height - 100)
SpriteView(scene: accelerometerView)
.frame(height: 50)
}
}
}