gpt4 book ai didi

iphone - Swift - 带有 SpriteNodes 和 UILabel 的 NSLayout 表

转载 作者:行者123 更新时间:2023-11-28 06:47:01 25 4
gpt4 key购买 nike

我仍在使用 Xcode 和 Swift 开发我的第一款 iPhone 游戏。

游戏运行良好,所以我认为我可以为游戏添加一些功能。其中之一是玩家可以看到他的统计数据的场景。

所以我为场景 (SKSpriteNodes) 创建了一个背景,并在场景中添加了一些 UILabels

对于 iPhone 6 Plus,第一个 UILabel 位于正确的位置(使用 NSLayout): iPhone 6 Plus

所以现在我已经在 iPhone 5 上对其进行了测试......它看起来像: 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?

最佳答案

是的,最近 apple 使这比以前更容易了。如果您在 Storyboard中,则可以从标签拖到其他标签或拖到背景本身。您可以静态分隔对象,或动态分组并均匀分隔对象。这是当您按住鼠标左键拖动到背景到 ScrollView 一侧时的样子: enter image description here和另一个对象(在本例中为按钮): enter image description here

您可以使用右下角的小按钮对您的定位进行特定调整: enter image description here

如果您希望以编程方式而不是通过 Storyboard 来执行此操作,那么您可以在 Storyboard 的“View Controller Scene”侧滚动条中找到用于编辑此类约束的变量名称。只需在 Storyboard 中举几个你想做的事的例子(之后你可以删除),你就会看到它们的变量名: enter image description here

关于iphone - Swift - 带有 SpriteNodes 和 UILabel 的 NSLayout 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36049565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com