无法使用锚点激活约束

Unable to activate constraint with anchors

所以,我正在尝试以编程方式创建 sceneView

class ViewController: UIViewController, ARSCNViewDelegate {
    var sceneView: ARSCNView = ARSCNView()
    let configuration = ARWorldTrackingConfiguration()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
        self.configuration.planeDetection = .horizontal
        self.sceneView.session.run(configuration)
        self.sceneView.delegate = self
        self.sceneView.autoenablesDefaultLighting = true
        
        //add autolayout contstraints
        self.sceneView.translatesAutoresizingMaskIntoConstraints = false
        self.sceneView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        self.sceneView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        self.sceneView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        self.sceneView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

    }
    
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard anchor is ARPlaneAnchor else {return}
    }
}

但我收到此错误消息:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x1c0278700 "ARSCNView:0x10690d7e0.top"> and <NSLayoutYAxisAnchor:0x1c426db40 "UIView:0x106b14e30.top"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'

它发生在 \add autolayout contstraints 部分。如何向该元素添加约束?

,需要添加sceneView作为子视图才能锚定。尝试这样的事情:

view.addSubview(sceneView)
sceneView.anchor(top: self.view.topAnchor, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)

这是一个使用布局轴锚点激活布局约束的绝妙解决方案,不会使您的 ViewController 混乱。这里下视图是NSView,上视图是SCNView。

import SceneKit

class ViewController: NSViewController {
    
    lazy var sceneView: SCNView = {
        let sceneView = SCNView(frame: .zero)
        sceneView.allowsCameraControl = true
        sceneView.scene = SCNScene(named: "art.scnassets/ship.scn")!
        return sceneView
    }()
           
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(sceneView)      // put it first
        self.viewAnchors(sceneView)          // put it next
        
        self.view.subviews.forEach {
            [=10=].translatesAutoresizingMaskIntoConstraints = false
        }
    }
}

将扩展名放入单独的 .swift 文件中是有意义的。

extension ViewController {

    func viewAnchors(_ sv: NSView) {

        NSLayoutConstraint.activate([
            sv.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            sv.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
            sv.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
            sv.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
        ])
    }
}