1
votes

I want to add a custom UIView with one UILabel into SCNNode (using SceneKit and ARKit).

I used SCNPlane with SCNMaterial containing created UIView's layer. Then I init new SCNNode and add it into scene. Firstly I tried UIView created in interface builder and this doesn't work. So I decided to create UIView programmaticaly.

class InfoPlaneNodeView: UIView {
    var elevationLabel: UILabel

    override init(frame: CGRect) {
        elevationLabel = UILabel(frame: frame)
        super.init(frame: frame)
        backgroundColor = UIColor.white
        layer.cornerRadius = 20

        elevationLabel.translatesAutoresizingMaskIntoConstraints = false
        elevationLabel.textColor = UIColor.black
        elevationLabel.text = "333m"
        addSubview(elevationLabel)

        elevationLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        elevationLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
    }
}

class InfoViewPlaneNode: SCNNode {
    init(planeWidth: CGFloat, planeHeight: CGFloat) {
        let material = SCNMaterial()
        let infoPlaneNodeView = InfoPlaneNodeView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
        material.diffuse.contents = infoPlaneNodeView.layer
        material.transparency = 0.7
        material.lightingModel = .constant
        material.isDoubleSided = true

        let plane = SCNPlane(width: planeWidth, height: planeHeight)
        plane.materials = [material]
        super.init()
        self.geometry = plane
        self.position = SCNVector3(0, 3, 0)
    }
}

When I run all the code, I see only white UIView without UILabel. I tried this code without SceneKit and everything works fine. Where is problem? Thank you

https://imgur.com/a/rQmKJX6

1

1 Answers

0
votes

Once I faced this case. Since view is not part of any view controller, you have to draw it manually:

view.setNeedsDisplay()
view.displayIfNeeded()