我有一个 UIView 和一些内置在函数 menuState 中的标签和按钮。当我单击 menuState 函数中的按钮时,它会将我们带出该函数,进入 menuPlayButtonClicked() 函数。在此函数中,我试图隐藏我在 menuState 函数中构建的标签和 UIView,因为游戏正在开始。使用 menuView.isHidden = true 并没有这样做。 menuView.removeFromSuperView() 也没有这样做。我也尝试使用 let menuView = UIView() 全局声明这些 View 和标签,但它仍然没有删除它。我错过了什么?我仍然可以在游戏组件下方的背景中看到标签和 UIView。
func menuState() {
//Build the menu box
let menuView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 400))
menuView.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
self.view.addSubview(menuView)
menuView.layer.zPosition = 1;
menuView.layer.cornerRadius = 10
menuView.isHidden = false
...
Also some code for a button here, which takes us to the setupGame function when clicked
}
最佳答案
全局声明的 menuView
对象与在名为“menuState()”的函数中本地创建的对象不同。所以,不要声明另一个本地实例,您可以尝试以下方式 -
menuState() {
menuView = ... // don't use `let` or 'var' again here, but reference the same global variable that you will use later to hide
}
关于ios - 从 View 中删除 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406429/