【发布时间】:2016-03-17 00:33:21
【问题描述】:
我仍在使用 Xcode 和 Swift 开发我的第一款 iPhone 游戏。
游戏运行良好,所以我想我可以为游戏添加一些功能。其中之一是玩家可以看到他的统计数据的场景。
所以我为场景 (SKSpriteNodes) 创建了一个背景,并在场景中添加了一些 UILabels。
对于 iPhone 6 Plus,第一个 UILabel 位于正确的位置 (using NSLayout):
所以现在我已经在 iPhone 5 上对其进行了测试,例如 .. 它的样子:
到目前为止我的代码..
func StatsLabelsAnzeigen(){
StatHoechsteScoreLabel.textColor = SKColor.darkGrayColor()
StatHoechsteScoreLabel.textAlignment = NSTextAlignment.Center
StatHoechsteScoreLabel.font = UIFont.systemFontOfSize(16)
StatHoechsteScoreLabel.alpha = 1
StatHoechsteScoreLabel.translatesAutoresizingMaskIntoConstraints = false
StatHoechsteScoreLabel.text = "\(HoechsteScore)" // (Int)
self.view?.addSubview(StatHoechsteScoreLabel)
// Create Constraints
let RechtsbuendigStatHoechsteScoreLabel:NSLayoutConstraint = NSLayoutConstraint(item: self.view!, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: StatHoechsteScoreLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 10)
let ObenbuendigStatHoechsteScoreLabel:NSLayoutConstraint = NSLayoutConstraint(item: StatHoechsteScoreLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 168)
let heightStatHoechsteScoreLabel = NSLayoutConstraint.constraintsWithVisualFormat("V:[StatHoechsteScoreLabel(40)]", options: [], metrics: nil, views: ["StatHoechsteScoreLabel" : StatHoechsteScoreLabel])
let widthStatHoechsteScoreLabel = NSLayoutConstraint.constraintsWithVisualFormat("H:[StatHoechsteScoreLabel(200)]", options: [], metrics: nil, views: ["StatHoechsteScoreLabel" : StatHoechsteScoreLabel])
// Add the created Constraints
StatHoechsteScoreLabel.addConstraints(heightStatHoechsteScoreLabel)
StatHoechsteScoreLabel.addConstraints(widthStatHoechsteScoreLabel)
self.view?.addConstraint(RechtsbuendigStatHoechsteScoreLabel)
self.view?.addConstraint(ObenbuendigStatHoechsteScoreLabel)
我的统计数据背景:
StatsSceneBGNode = SKSpriteNode(imageNamed: "StatsBG2")
StatsSceneBGNode.size = CGSize(width: view.frame.size.width*80/100, height: view.frame.size.height*80/100)
StatsSceneBGNode.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
StatsSceneBGNode.zPosition = 2
StatsSceneBGNode.name = "StatsSceneBGNode"
addChild(StatsSceneBGNode)
是否有可能使约束更加灵活,使其看起来适合每部 iPhone?
【问题讨论】:
标签: iphone xcode swift uilabel nslayoutconstraint