ios - 无法使用 anchor 激活约束

标签 ios swift scenekit arkit sceneview

所以,我正在尝试以编程方式创建一个 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 constraints 部分。如何向该元素添加约束?

最佳答案

dan is right ,你需要添加 sceneView 作为 subview 才能锚定它。尝试这样的事情:

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)

关于ios - 无法使用 anchor 激活约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47168059/

相关文章:

ios - 在 iphone 中管理 hls native 行为?

ios - 由于应用程序标识符授权,应用程序安装失败

ios - Codables 错误与 Alamofire "Cannot convert value of type ' [字符串 : Any ]' to expected argument type ' Data'"

objective-c - Swift 中的 CFNetwork 错误处理

ios - 如何用 6 个三角形 SCNNode 制作六边形?

ios - SWIFT: "didBeginContact"函数中的 If 语句运行次数过多

ios - CAShapeLayer上的逆时针行程

swift - 您如何为当前平台使用合适的颜色类别?

ios - 旋转2个盒子相交

ios - 使用混合 View Controller 、3d 场景工具包、uiwebkit 捕获屏幕