如何正确调整SKView子视图和SKScene的大小

时间:2016-04-12 01:53:50

标签: ios swift uistoryboard skview

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

我正在使用故事板创建UIView,以及SKView的子视图。我想以编程方式创建一个SKScene,其尺寸为SKView

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

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

iPhone 6屏幕截图(肖像)
iPhone 6 Screenshot (portrait)

iPhone 6屏幕截图(风景) iPhone 6 Screenshot (landscape)

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

使用SKView参考的ViewController 这是我创建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)
    }
}

Main.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)
        }

1 个答案:

答案 0 :(得分:1)

事实证明我的约束没有正确设置。

<强>约束
ios view constraints

<强>结果
iPhone 4s portrait