如何使用 swift 中的 CGPOINT 将标签贴在横向屏幕的右上角?

How do I get my label to stick to the top right of my landscape screen with CGPOINT in swift?

首先,我是 swift 的新手,我正在尝试创建一个标签,在屏幕右上角显示我的播放器 class 的值。如何在右上角制作一个粘性标签,当用户在屏幕上平移时它会留在那儿?

我的最终目标是创建类似于此部落冲突示例的内容:

到目前为止我已经创建了这个并且不希望它位于中间,相反我感兴趣的是将它的位置改变到一边并在用户滚动时保持在那里。

func layoutScene() {
    scoreLabel.fontName = "AvenirNext-Bold"
    scoreLabel.fontSize = 60.0 
    scoreLabel.fontColor = UIColor.white
    scoreLabel.position = CGPoint(x: frame.midX, y: frame.midY)
    scoreLabel.zPosition = 1;
    addChild(scoreLabel)
}

就用这个:

scoreLabel.position = CGPoint(x: frame.maxX - scoreLabel.frame.width - 16, y: frame.maxY - 16)

要将标签贴在视图顶部:

1) 您需要在 view.Set 此视图的顶部添加透明 UIView,并使用 supperView 提供前导和顶部约束,提供高度、宽度并将 UIColor 设置为清晰。

2) 将你的按钮设置到你的透明 UIView 中并给它适当的约束。

Note: If you are using scrollView or any other view and trying to stick your button on it. Give the constraint to your transparent view with main view(self.view) so another subviews underneath your transparent view will stay free from it.