ios - 如何正确调整 SKView subview 和 SKScene 的大小

标签 ios swift uistoryboard skview

我正在尝试获取/设置 subview (SKView) 的正确大小。

我正在使用 Storyboard创建一个 UIView 和一个 SKView subview 。我想以编程方式创建一个具有 SKView 尺寸的 SKScene

我的想法是 scene.size.heightscene.size.width 将等于 SKView 的高度和宽度。为了测试这一点,我在每个角上画了四个蓝色圆圈,在边框上画了红线。当我期望看到所有四个蓝色角点和边界线时,我只能看到左下角。

请忽略场景中的黑色圆圈,它们无关紧要。

iPhone 6 截图(纵向)
iPhone 6 Screenshot (portrait)

iPhone 6 屏幕截图(横向) iPhone 6 Screenshot (landscape)

我添加了 SW(西南)、SE、NE 和 NW 标签

ViewController 与 SKView 引用 这是我创建 SKSCene 的地方(参见 func newGame)

import UIKit
import SpriteKit


class CenterView: UIViewController, ActionDelegate {
    @IBOutlet weak private var navBar:UINavigationBar!
    @IBOutlet weak private var titleBar:UINavigationItem!
    @IBOutlet weak private var gameView:SKView!

    var navigation:NavigationDelegate?
    var action:ActionDelegate?
    var game:GameDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.action = self
        newGame()
    }

    @IBAction func menuClick(sender: AnyObject) {
        navigation?.toggleLeftPanel()
    }

    func setTitleBarTitle(title: String) {
        titleBar.title = title
    }

    func newGame() {
        print("skview bounds: \(self.gameView.bounds.size)")
        let game = GameScene(size: self.gameView.bounds.size)
        self.game = game
        game.action = action
        game.scaleMode = .ResizeFill
        self.gameView.presentScene(game)
    }
}

主 Storyboard Main.storyboard

约束

Constraints

添加角圆和边框线

if let scene = self.scene {
            let dot = SKShapeNode(circleOfRadius: 10)
            dot.fillColor = UIColor.blueColor()
            dot.position = CGPoint(x: 0,y: 0)

            let dot1 = SKShapeNode(circleOfRadius: 10)
            dot1.fillColor = UIColor.blueColor()
            dot1.position = CGPoint(x: scene.size.width,y: 0)

            let dot2 = SKShapeNode(circleOfRadius: 10)
            dot2.fillColor = UIColor.blueColor()
            dot2.position = CGPoint(x: 0,y: scene.size.height)

            let dot3 = SKShapeNode(circleOfRadius: 10)
            dot3.fillColor = UIColor.blueColor()
            dot3.position = CGPoint(x: scene.size.width,y: scene.size.height)


            let left = SKShapeNode(rect: CGRect(x: 0, y: 0, width: 3, height: scene.size.height))
            let top = SKShapeNode(rect: CGRect(x: 0, y: scene.size.height, width: scene.size.width, height: 3))
            let right = SKShapeNode(rect: CGRect(x: scene.size.width, y: 0, width: 3, height: scene.size.height))
            let bottom = SKShapeNode(rect: CGRect(x: 0, y: 0, width: scene.size.width, height: 3))

            left.fillColor = UIColor.redColor()
            top.fillColor = UIColor.redColor()
            bottom.fillColor = UIColor.redColor()
            right.fillColor = UIColor.redColor()


            scene.addChild(dot)
            scene.addChild(dot1)
            scene.addChild(dot2)
            scene.addChild(dot3)

            scene.addChild(left)
            scene.addChild(top)
            scene.addChild(right)
            scene.addChild(bottom)
        }

最佳答案

原来是我的约束没有设置正确。

约束
ios view constraints

结果
iPhone 4s portrait

关于ios - 如何正确调整 SKView subview 和 SKScene 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36562273/

相关文章:

iphone - 无法从 super View 中删除图像

swift - 如何实现 unarchiver 委托(delegate)来覆盖类名?

swift - 我的自定义单元格不会移动,我添加的所有内容都会卡在左墙上

ios - Xcode 链接器命令失败,退出代码为 1(使用 -v 查看调用)

ios - 是否可以再运行 iOS 4.0 模拟器?

ios - 如何在 Firebase 中设置属性?

ios - 保存自定义对象数组

ios - 使用专用类用 iPhone 模拟信标

swift - ScrollView 中的图像不可滚动

ios表格单元格左侧细节不是动态大小