How to position an SKLabelNode on top of a UISlider in SpriteKit

88 Views Asked by At

I'm using UISliders in my SpriteKit game.

Sometimes I want to pause the game and add some info in an SKLabelNode but I can't find a way to position the SKLabelNode on top of a UISlider.

Here's a simplified example.

   override func didMove(to view: SKView) {

let slider = UISlider(frame: CGRect(x: 100, y: 100, width: 300, height: 50))

let label = SKLabelNode(text: "Text Label should be on top of Slider")
label.fontColor = .red

label.verticalAlignmentMode = .top
label.position = CGPoint(x:160, y:self.frame.size.height - 110)


view.addSubview(slider)
self.addChild(label)

slider.layer.zPosition = 1
label.zPosition = 2

}

UISlider remains above SKLabel :-(. Any help appreciated.

EDIT: In response to question by @Muffinman2497 "Why not use a UILabel?"

I may have to do that but I'm actually using a custom multi line label that's been designed using spriteKit and SKLabels. So I was hoping for a simple property change rather than a redesign.

Using a UILabel definitely does work though.

 let myUILabel = UILabel(frame: CGRect(x: 50, y: 0, width: 300, height: 50))
myUILabel.text = "UILabel would like to obscure UISlider"
myUILabel.backgroundColor = .red
slider.addSubview(myUILabel)
slider.layer.zPosition = 1
myUILabel.layer.zPosition = 2
0

There are 0 best solutions below