upl*_*com 1 sprite-kit xcode12beta5
出于某种原因,我的代码不会填满整个 SKScene。这是我在 Xcode 12 Beta 5 上使用的代码。
GameScene.swift
class GameScene: SKScene {
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "space")
background.zPosition = 0
background.anchorPoint = CGPoint(x: 0.5, y: 0.5) // default
background.position = CGPoint(x: frame.midX, y: frame.midY)
print("frame.size \(frame.size)")
print("self.size \(self.size)")
print("view \(view.frame.size)")
background.size = CGSize(width: self.size.width, height: self.size.height)
self.addChild(background)
}
}
Run Code Online (Sandbox Code Playgroud)
GameViewController.swift
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GKScene(fileNamed: "GameScene") {
if let sceneNode = scene.rootNode as! GameScene? {
// Present the scene
if let view = self.view as! SKView? {
sceneNode.size = view.bounds.size
sceneNode.anchorPoint = CGPoint.zero
sceneNode.scaleMode = .aspectFit
print("view.bounds.size \(view.bounds.size)")
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
}
override var shouldAutorotate: Bool {
return true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}
override var prefersStatusBarHidden: Bool {
return true
}
}
Run Code Online (Sandbox Code Playgroud)
同样出于某种原因,我的视图大小正在报告 ->
frame.size (320.0, 480.0) self.size (320.0, 480.0) 视图 (320.0, 480.0)
但是我的 GameScene.sks 设置为 -> w750, h1336
Luc*_*ahl 10
这听起来很愚蠢,但是您有启动屏幕吗?我遇到了同样的问题,这只发生在 Xcode 12 而不是 11,但我发现的主要区别是 Xcode 11 有一个启动屏幕。一旦我添加了启动屏幕故事板并将其添加到我的 plist 中,我的 SCNView 就会填满屏幕。我不确定为什么这会导致视图不遵循约束,但是在将它添加到我的其他项目后它解决了这个问题。
编辑:您不需要启动屏幕故事板,您只需在“启动屏幕界面文件基本名称”下的 plist 中添加显示场景的主故事板即可。