ios - 从 View 中删除 UIView

标签 ios swift uiview swift3 uilabel

我有一个 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/

相关文章:

ios - 是否可以通过 Springboard 调整在 SBApplication 中设置 userInfo?

ios - UIImage 缓存 : Performance Consequences Only?

swift - Reality Composer 默认 anchor

swift - 快速协议(protocol)之间的循环

ios - 添加应用组后 Xcode 显示警告(将 "App Groups"授权添加到您的应用 ID)

ios - 如何快速读取 UITextInput 中的文本?

swift - @ObjectBinding 不适用于协议(protocol)

ios - 如何反转 UIView 或层下所有 View 的颜色?

uiview - 将 UIView 更改为 UIScrollView

ios - UIView & UIViewController 应用程序设计