[英]Unable to activate constraint with anchors
所以,我正在嘗試以編程方式創建一個場景視圖
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:04x306 nocommonancestorage" . 約束或其錨點是否引用了不同視圖層次結構中的項? 那是非法的。
它發生在\\\\add autolayout contstraints
部分。 如何向該元素添加約束?
dan 是對的,您需要將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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.