[英]Center SKView in UIViewController: Swift
I'm using the following code to load an SKView
on the view controller. 我正在使用以下代码在视图控制器上加载SKView
。 However, when you first load the app the buttons, labels etc. In the scene show up at the very bottom left. 但是,当您第一次加载应用程序时,按钮,标签等在场景中显示在最左下方。 But, in the game when you return to the scene, it's centered, how could I fix this? 但是,在你回到场景的游戏中,它是居中的,我怎么能解决这个问题呢?
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
}
}
I suggest moving the your code from viewDidLoad to 我建议将你的代码从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 will be called whenever the view reposition itself or get resized 只要视图重新定位自身或调整大小,就会调用viewDidLayoutSubviews
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.