无法使用锚点激活约束 [英] Unable to activate constraint with anchors

查看:31
本文介绍了无法使用锚点激活约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试以编程方式创建一个场景视图

So, I'm trying to create a sceneView programatically

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}
    }
}

但我收到此错误消息:

由于未捕获的异常NSGenericException"而终止应用程序,原因:无法使用锚点激活约束 <NSLayoutYAxisAnchor:0x1c0278700 "ARSCNView:0x10690d7e0.top>>和 <NSLayoutYAxisAnchor:0x1c426db40 "UIView:0x106b14e30.top>因为他们没有共同的祖先.约束或其锚点是否引用了不同视图层次结构中的项?那是非法的.'

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 部分.如何向该元素添加约束?

It's happens in the part \\add autolayout contstraints. How can I add constraints to that element?

推荐答案

dan 说得对,需要先添加sceneView作为子视图,然后才能锚定它.尝试这样的事情:

dan is right, you need to add sceneView as a subview before you can anchor it. Try something like this:

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)

这篇关于无法使用锚点激活约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆