简体   繁体   中英

How can I add a navigation button to a sceneView?

I have an app that has multiple views and one of them is a SceneView as you can see below. The SceneView is not the initial view. Users can open the SceneView by triggering a segue from another ViewController but when I want to add a nav bar to the SceneView, an error has occured.

So how can I add a navigation bar or a button to a sceneView? If there is no way, how can I manage to dismiss segue from the SceneView?

在此处输入图像描述

I couldn't find a way to add a navigationBar on top of a sceneView but I figured out how to add a button to a sceneView . If we create a button programmatically and add that button to the sceneView, we can navigate via that button.

Here's what we can do;

import UIKit
import SceneKit

class ViewController: UIViewController {

var scnView: SCNView?
var exampleScn = SCNScene(named: "ExampleScn")

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.scnView = SCNView(frame: self.view.frame)
    self.scnView?.scene = exampleScn

    self.view.addSubview(self.scnView!)

    let button = UIButton(type: .system)
    button.tintColor = UIColor.black
    button.titleLabel?.font = UIFont(name: "Arial", size: 25)
    button.setTitle("Back", for: .normal)
    button.sizeToFit()
    button.addTarget(self, action: #selector(didPressBack), for: .touchUpInside)
    button.center.x = 50
    button.frame.origin.y = 20
    scnView.addSubview(button)
}

@objc func didPressBack (sender: UIButton!) {
    dismiss(animated: true, completion: nil)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

if we do this, the result will look like this:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM