ios - 将 ViewController 设置为彼此重叠且不透明。

标签 ios swift viewcontroller

我是一位新的 Swift 编写者,正在寻找一个相对具体问题的答案。新手的错误还请多多包涵。

我正在尝试以编程方式在屏幕上创建一个弹出窗口,将两个 View Controller 分层,以便一个 View Controller 在另一个 View Controller 上可见,并具有一定程度的不透明度,使背景可见。

从我的 GameViewController 进行转换后,请参见此处:

let gameOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "gameOverID") as! GameOverViewController
    self.addChildViewController(gameOverVC)
    gameOverVC.view.frame = self.view.frame
    self.view.addSubview(gameOverVC.view)
    gameOverVC.didMove(toParentViewController: self) 
}

...我实例化了 GameOverViewController 的 ViewController 以适应 GameViewController。之后,我转到 GameOverViewController 类并尝试使用以下代码行将背景颜色设置为不透明:

self.view.backgroundColor = UIColor.black.withAlphaComponent(0.8)

问题是我最终得到的是一个不透明的背景,而实际上我宁愿 GameViewController 分层在具有不透明背景的 GameViewController 之上,以便仍然能够看到游戏。

我还在 GameOverViewController 中使用以下内容制作动画:

 func showAnimate()
{
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    })
}

func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
        self.view.alpha = 0.0
    }, completion: { (finished: Bool) in
        if (finished)
            {
            self.view.removeFromSuperview()
        }
    })
}

我不确定问题的根源是什么。

感谢您的帮助。

最佳答案

在界面生成器中更改游戏 View Controller 的背景颜色,如下所示

enter image description here

另外,请注意,您在removeAnimate函数的这一行中删除了游戏viewController的 View

   self.view.removeFromSuperview()

如果您在通过 VC 显示游戏之前执行此操作,可能会导致黑色背景

关于ios - 将 ViewController 设置为彼此重叠且不透明。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48287058/

相关文章:

iphone - 存储图像的最佳位置是什么

iphone - UIView block 动画转换与显示/隐藏键盘上的动画内容

swift - 无法将类型 'String?' 的值分配给类型 'String'

objective-c - 如何改变rootViewController的 View ?

ios - writeToFile:atomically: 方法追加到当前现有文件?

iphone - UIPickerViews 不显示

ios - 字符串中的内联按钮和/或文本字段,使用 Swift 包装与文本相同的文本

ios - 将两个 imageView 合并为一个并保存 iOS swift

swift - 鼠标移动和 ViewController

ios - prepareForSegue :sender: fails to alter property on destination VC