gpt4 book ai didi

ios - 无法以编程方式复制约束

转载 作者:行者123 更新时间:2023-11-28 06:22:16 26 4
gpt4 key购买 nike

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

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

最佳答案

设置一次约束(例如在 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() 进行打印,因为我知道这将在所有 View 上调用 updateConstraints() 之后发生。

关于ios - 无法以编程方式复制约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43027658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com