如何添加按钮布局约束ARSKView?

时间:2017-09-26 15:53:44

标签: swift xcode uikit ios11 arkit

我使用带有UIKit元素的SpriteKit(ARSKView),并且我想要一直添加一个停留在屏幕左下角的按钮。按下时,按钮将清除所有节点。

这是我试图让按钮显示的代码,但是当我尝试添加底部锚点和左锚点约束时,我收到此错误

  

无法使用锚点激活约束    因为他们没有共同的祖先。   约束或其锚点是否在不同视图中引用项目   层次结构?这是非法的。'

点击时按钮工作正常,但如何在ARSKView中添加布局约束?

并且如何让按钮清除场景中的所有节点?

这是我的代码

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the view's delegate
    sceneView.delegate = self

    // Show statistics such as fps and node count
    sceneView.showsFPS = true
    sceneView.showsNodeCount = true
 //   sceneView.showsPhysics = true

    // Load the SKScene from 'Scene.sks'
    if let scene = SKScene(fileNamed: "Scene") {
        sceneView.presentScene(scene)



    }


    let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
    button.backgroundColor = UIColor(red: 0, green: 0, blue:0, alpha: 0.4)
    button.setTitle("Clear Screen", for: .normal)
    button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    button.bottomAnchor.constraint(equalTo: sceneView.bottomAnchor).isActive = true
    button.leftAnchor.constraint(equalTo: sceneView.leftAnchor).isActive = true
    self.view.addSubview(button)


}

@objc func buttonAction(sender: UIButton!) {
    print("Button tapped")

}

0 个答案:

没有答案
相关问题