无法以编程方式复制约束

时间:2017-03-26 10:36:45

标签: ios swift

我想在我的代码中以编程方式添加这些约束:

enter image description here

到目前为止,我得到了这个:

    self.view.bringSubview(toFront: sceneView)
    sceneView.bottomAnchor.constraint(equalTo: canvasUnderReference.topAnchor, constant: 0).isActive = true
    sceneView.topAnchor.constraint(equalTo: playersScoreReference.bottomAnchor, constant: 0).isActive = true
    sceneView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    sceneView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
    print(sceneView.frame)

打印出来:

(0.0, 0.0, 0.0, 0.0)

为什么打印出这些值?是否没有添加约束?

我在viewDidLoad和这个函数中尝试了它:

override func viewDidLayoutSubviews() {
    sceneView.backgroundColor = UIColor.clear
    sceneView.ignoresSiblingOrder = true
    self.view.addSubview(sceneView)
    sceneScene = SKScene(fileNamed: "MyScene")!
    sceneScene.backgroundColor = .white
    sceneScene.scaleMode = .resizeFill
    sceneView.showsNodeCount = true
    let canvasUnderReference = CanvasUnder.layoutMarginsGuide
    let playersScoreReference = playersScore.layoutMarginsGuide
    sceneView.bottomAnchor.constraint(equalTo: canvasUnderReference.topAnchor, constant: 0).isActive = true
    sceneView.topAnchor.constraint(equalTo: playersScoreReference.bottomAnchor, constant: 0).isActive = true
    sceneView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    sceneView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
    print(sceneView.frame)
    sceneView.presentScene(sceneScene)
    self.view.bringSubview(toFront: CanvasCards)
    self.view.bringSubview(toFront: sceneView)

}

编辑2:

override func viewDidLoad()
    {
        super.viewDidLoad()

        sceneView.backgroundColor = UIColor.clear
        sceneView.ignoresSiblingOrder = true
        self.view.addSubview(sceneView)
        sceneScene = SKScene(fileNamed: "MyScene")!
        sceneScene.backgroundColor = .white
        sceneScene.scaleMode = .resizeFill
        sceneView.showsNodeCount = true
        let canvasUnderReference = CanvasUnder.layoutMarginsGuide
        let playersScoreReference = playersScore.layoutMarginsGuide
        sceneView.bottomAnchor.constraint(equalTo: canvasUnderReference.topAnchor, constant: 0).isActive = true
        sceneView.topAnchor.constraint(equalTo: playersScoreReference.bottomAnchor, constant: 0).isActive = true
        sceneView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        sceneView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        sceneView.presentScene(sceneScene)
        self.view.bringSubview(toFront: CanvasCards)
        self.view.bringSubview(toFront: sceneView)
}

它仍会打印0,0,0,0

1 个答案:

答案 0 :(得分:1)

设置约束一次(例如在viewDidLoad()中)。在调用updateConstraints()之后,约束不会影响帧( 不要调用updateConstraints(),系统执行此操作 )。

此外,宽度约束不应设置为常量。您想将其约束为self.view

override func viewDidLoad() {
    super.viewDidLoad()

    sceneView.bottomAnchor.constraint(equalTo: canvasUnderReference.topAnchor, constant: 0).isActive = true
    sceneView.topAnchor.constraint(equalTo: playersScoreReference.bottomAnchor, constant: 0).isActive = true
    sceneView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    // This is the proper way to create width constraint.
    sceneView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    print(sceneView.frame)
}

我使用viewDidLayoutSubviews()进行打印,因为我知道在所有视图上调用updateConstraints()后会发生这种情况。