gpt4 book ai didi

ios - Swift:添加 subview 后背景消失

转载 作者:行者123 更新时间:2023-11-28 06:48:17 26 4
gpt4 key购买 nike

我有一个 GameOver 类,它显示背景颜色和一个播放按钮。添加“view.addSubview(self.playAgainButton)”后,背景色弹出并突然从屏幕上消失。再次播放按钮仍然存在。

如果我删除 addSubview 调用,背景颜色会保留在那里。

谁能帮帮我?

class GameOver: SKScene{
var playAgainButton = UIButton()
var playAgainButtonImage = UIImage(named: "PlayAgainButton")

override func didMoveToView(view: SKView) {
backgroundColor = UIColor.redColor()

self.playAgainButton = UIButton(type: UIButtonType.Custom)
self.playAgainButton = setImage(playAgainButtonImage, forState: .Normal)
self.playAgainButton.frame = CGRectMake(self.frame.size.width / 2, self.frame.size.height / 2, 190, 70)
self.playAgainButton.layer.anchorPoint = CGPointMake(1.0, 1.0)
self.playAgainButton.layer.zPosition = 0

// If I add this line -> backgroundcolor disappears
view.addSubview(self.playAgainButton)

}
}

我的 GameScene gameOver 函数看起来像这样(也许有错误?)

func gameOver() {
let skView = self.view! as SKView
skView.ignoresSiblingOrder = true

self.scene?.removeFromParent()

let transition = SKTransition.fadeWithColor(UIColor.grayColor(), duration: 1.0)
transition.pausesOutgoingScene = false

var scene: GameOver!
scene = GameOver(size: skView.bounds.size)

scene.scaleMode = .AspectFill

skView.presentScene(scene, transition: transition)
}

最佳答案

你必须将 subview 添加到父 View

改变

view.addSubview(self.playAgainButton)

view.superview?.addSubview(self.playAgainButton)

解决问题

关于ios - Swift:添加 subview 后背景消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632142/

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