Swift:addSubview后背景消失了

时间:2016-02-25 15:53:46

标签: ios swift uibutton sprite-kit uikit

我有一个GameOver类,它显示backgroundcolor和playAgain按钮。 添加“view.addSubview(self.playAgainButton)”后,backgroundcolor弹出,然后突然从屏幕上消失。 playAgain-Button仍在那里。

如果我删除了addSubview Call,那么backgroundcolor就会停留在那里。

有人能帮助我吗?

mycoolwebsite.com/profile

我的GameScene gameOver功能看起来像这样(可能有错误?)

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)

  }
}

1 个答案:

答案 0 :(得分:1)

您必须将子视图添加到超级视图

更改

view.addSubview(self.playAgainButton)

view.superview?.addSubview(self.playAgainButton)

解决问题