Black screen on ARView

Viewed 412

I am trying to programmatically instantiate an ARView. This is my code for the View Controller

import Foundation
import UIKit
import RealityKit

class ARViewController: UIViewController {
  
  var arView: ARView = {
    let arview = ARView()
    arview.cameraMode = .ar
    arview.automaticallyConfigureSession = true
    arview.translatesAutoresizingMaskIntoConstraints = false
    return arview
  }()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
    setupUIConstraints()
  }
  
  func setupUI() {
    view.addSubview(arView)
    
    // Load the "Cupcake" scene from the "Experience" Reality File
    let boxAnchor = try! Experience.loadBox()
    
    // Add the box anchor to the scene
    arView.scene.anchors.append(boxAnchor)
  }
  
  func setupUIConstraints() {
    view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    view.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
    view.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
  }
}

The app will appropriately ask me for Camera permissions, but even after accepting, the 'view' is just a black screen. I am on an iPhone XS, and have imported arkit into my info.plist. Any help would be much appreciated.

1 Answers

In setupUIConstraints you're setting the constraints for the ARViewController.view to its own constraints, rather than the ARView.

Change to the ARView and you should be good!

Related