Reputation: 337
What is the best way to add a label at the right or left top corner of the scene? It should be cross-device.
let height = self.frame.size.height
let width = self.frame.size.width
label.fontSize = 50
label.verticalAlignmentMode = .Top
label.horizontalAlignmentMode = .Left
label.position = CGPoint(x:2, y:height-2)
But it would not be in the corner. What is wrong?
Upvotes: 3
Views: 3914
Reputation: 130193
In Sprite Kit, the default origin of the scene's coordinates is in the bottom left corner of the screen. So, if you want the label in a top corner, you should keep using the screen's height for your label's position, but also use either 0 or the screen's width for the x position. Coupling this with a horizontal alignment mode of left or right depending on the side, and your label will be in the correct position.
For a label in the top left corner:
label.horizontalAlignmentMode = .Left
label.position = CGPoint(x:0.0, y:self.size.height)
Or for the top right:
label.horizontalAlignmentMode = .Right
label.position = CGPoint(x:self.size.width, y:self.size.height)
Upvotes: 2