模态视图控制器不包括状态栏

Ede*_*n.F 13 iphone xcode ios swift

我正在开发一个ios应用程序.我有一个主视图,在这个视图中我试图呈现一个模糊的视图控制器与暗灰色的背景(黑色与不透明度).问题是状态栏不受此颜色的影响并保持不变.

这是我呈现视图控制器的方式:

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
            }
Run Code Online (Sandbox Code Playgroud)

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

这是问题(状态栏颜色): 问题说明 这是故事板中的模态视图: 故事板

Sul*_*han 8

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

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)
Run Code Online (Sandbox Code Playgroud)

但请注意,您应该通过视图的根控制器进行演示.有时,从内部控制器呈现时,您可能会产生奇怪的效果:

self.view.window?.rootViewController?.present(viewController, animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)

还要确保使用正确的modalPresentationStyle.

例


Jui*_*uit 0

当我UIViewController用 alpha != 1.呈现时,这段代码对我有用,UIViewController如下所示:

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "AddComment") as! AddCommentViewController
destinationVC.modalPresentationStyle = .overCurrentContext //this line is important
destinationVC.delegate = self
destinationVC.restId = self.restaurant.id
self.present(destinationVC, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

然后在destinationVC视图控制器中

override func viewWillDisappear(_: Bool) {
        UIView.animate(withDuration: 1, animations: { () in
            self.view.backgroundColor = .clear
        })
        super.viewWillDisappear(true)
    }

override func viewWillAppear(_: Bool) {
    UIView.animate(withDuration: 1, animations: { () in
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
    })
    super.viewWillAppear(true)
}
Run Code Online (Sandbox Code Playgroud)

并将其设置backgroundColor.clearinviewDidLoad或 Storyboard。因此UIViewController覆盖整个屏幕,包括状态栏。