How to attach a struct to the main GameViewController?
This struct occupies the top half of the main ViewController.
The goal is to have this struct be part of the main GameViewController
and not in separate UIHostingController
.
SO, when I rotate the main SKScene for example the struct also rotates OR when I switch SKScenes both the top-half struct and the bottom-half switch together because they are one SKScene.
The programmer who invented HStacks
and VStacks
is a blooming genius … but I need to avoid what I have now which is the HStack
VStack
combo closing and then its neighbors in the main SKScene closing.
Here are the essential code segments - all in my GameViewController
:
Declared at the top:
var useGSO = false
var gsoChildVC: UIViewController! // = UIHostingController
override func viewDidLoad() {
showScene()
/*
"gso" = "Game Static Objects"
*/
if useGSO {
// create a UIHostingController and extract a rootView for later processing
gsoChildVC = UIHostingController(rootView: GameStaticObjects())
}
} // viewDidLoad
Within my showScene()
:
func addGamePieces(toScene: SKScene) {
if thisSceneName == "GameScene" {
// various SKSpriteNodes
//
// STATIC People, Animals and Buildings
// are now added to the top of our SKScene
//
addGSOChild(toScene: toScene)
} // if thisSceneName == "GameScene"
else {
removeGSOChild()
}
} // addGamePieces
func addGSOChild(toScene: SKScene) {
// gsoChildVC = nil *if* useGSO = false
guard gsoChildVC == nil else {
let gsoParentVC = self // parent = us = GameViewController
// default value
// gsoChildVC.view.translatesAutoresizingMaskIntoConstraints = true
/**/
let sameWidth = gsoParentVC.view.bounds.width
let newHeight = 0.5 * gsoParentVC.view.bounds.height
let boundsWithNewBottom = CGRectMake(0.0, 0.0, sameWidth, newHeight)
gsoChildVC.view.frame = boundsWithNewBottom
/**/
// first, add the view of the hild to the view of the parent
gsoParentVC.view.addSubview(gsoChildVC.view)
// then, add the child to the parent
gsoParentVC.addChild(gsoChildVC)
// notify the child ViewController that we're finished
gsoChildVC.didMove(toParent: gsoParentVC)
return
}
} // addGSOChild
func removeGSOChild() {
// gsoChildVC = nil *if* useGSO = false
guard gsoChildVC == nil else {
// First, remove the Child from its Parent
gsoChildVC.removeFromParent()
// Then, remove the Child's view from the Parent's
gsoChildVC.view.removeFromSuperview()
return
}
} // removeGSOChild
As stated at the top of this OP, the HStack
and Stack
code appears great .. but the UIHostingController
is distinct from my GameViewController
. As a direct result, such operations as switching SKScene
s is done in 2 halves - (1) the HStack
and Stack
top half and (2) whatever non-struct
stuff appears below it.
If I switch SKScenes
, I want both halves to switch as one entity.
FWIW, the struct
that contains the HStack
and Stack
code is outside the GameViewController
class as suggested by Apple.
and as specified above, I call:
gsoChildVC = UIHostingController(rootView: GameStaticObjects())
within my viewDidLoad()
method.
I would love some basic guidance here as to what basic error I am making.