我正在使用以下代码在视图控制器上加载SKView
。但是,当您第一次加载应用程序时,按钮,标签等在场景中显示在最左下方。但是,在你回到场景的游戏中,它是居中的,我怎么能解决这个问题呢?
import UIKit
import SpriteKit
class ViewController: UIViewController {
override func loadView() {
self.view = SKView()
}
override func viewDidLoad() {
super.viewDidLoad()
let scene = StartScene(size: view.bounds.size)
let skView = view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .resizeFill
skView.presentScene(scene)
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
答案 0 :(得分:0)
我建议您将代码从 viewDidLoad 移至
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let scene = StartScene(size: view.bounds.size)
let skView = view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .resizeFill
skView.presentScene(scene)
}
每当视图重新定位或调整大小时,都会调用viewDidLayoutSubviews