ios - 模态视图 Controller 不覆盖状态栏

标签 ios iphone xcode swift

我正在开发 iOS 应用程序。我有一个主视图,在这个 View 中 我正在尝试呈现一个背景变暗(黑色不透明)的模态视图 Controller 。 问题是状态栏不受此颜色影响并保持不变。

这就是我呈现 View Controller 的方式:

let shareViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ShareViewController") as! ShareViewController
            shareViewController.battle = battle
            shareViewController.delegate = self
            let animation = CATransition()
            animation.duration = 1
            animation.type = kCATransitionFade
            self.view.window?.layer.addAnimation(animation, forKey: kCATransition)
            presentViewController(shareViewController, animated: false) {
               () in 
               // nothing here
            }

以下是演示问题的一些屏幕截图:

这就是问题所在(状态栏颜色): Problem illustration 这是 Storyboard 中的模态视图: storyboard

最佳答案

我无法重现您的问题,以下代码在我的单 View 应用中没有问题:

let viewController = UIViewController()
viewController.modalPresentationStyle = .overFullScreen
viewController.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)

let animation = CATransition()
animation.duration = 1
animation.type = kCATransitionFade
self.view.window?.layer.add(animation, forKey: kCATransition)

self.present(viewController, animated: false, completion: nil)

但是请注意,您应该在 View 的根 Controller 上呈现。有时,当您从内部 Controller 进行演示时,您会得到奇怪的效果:

self.view.window?.rootViewController?.present(viewController, animated: false, completion: nil)

还要确保您使用的是正确的 modalPresentationStyle

Example

关于ios - 模态视图 Controller 不覆盖状态栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35491746/

相关文章:

ios - 无法在 UIImagePickerController didFinishPickingMediaWithInfo : 中获取开始和结束编辑时间

ios - 屏幕方向更改后页面右侧的空白区域

ios - 我应该如何处理静态库中的依赖项?

javascript - 在 tableView Swift 中使用 javascript 数据

ios - 在 iOS 上从 RIFF 容器读取 AAC 音频

java - Android GTFS 应用程序

iphone - OpenSSL解密失败但错误代码为0

ios - Xcode: "No such module"嵌入式框架

swift - 如何检测cgRect中的cgPath

ios - xcode 在 iPhone 设备上运行应用程序时出错