0

我的代码使用了 inappview 依赖项,但抛出了这个警告: iOS 9.0 中不推荐使用 Setter for 'statusBarStyle': Use -[UIViewController preferredStatusBarStyle]

如何将此弃用的代码转换为新版本的代码?

    public func hide() {
    isHidden = true
    
    // Run later to avoid the "took a long time" log message.
    DispatchQueue.main.async(execute: {() -> Void in
        self.presentingViewController?.dismiss(animated: true, completion: {() -> Void in
            self.tmpWindow?.windowLevel = UIWindow.Level(rawValue: 0.0)
            UIApplication.shared.delegate?.window??.makeKeyAndVisible()
            if self.previousStatusBarStyle != -1 {
                UIApplication.shared.statusBarStyle = UIStatusBarStyle(rawValue: self.previousStatusBarStyle)!
            }
        })
    })
}
4

1 回答 1

0

覆盖preferredStatusBarStyle视图控制器中的方法,并调用setNeedsStatusBarAppearanceUpdate方法来更新状态栏的外观。你也可以让你previousStatusBarStyle的属性是可选的,这样你就可以使用 if-let 来检查它的可用性。prefersStatusBarHidden如果要隐藏状态栏,则覆盖方法返回 true。

class YourViewController : UIViewController {
    var previousStatusBarStyle: UIStatusBarStyle?

    public func hide() {
        isHidden = true

        // Run later to avoid the "took a long time" log message.
        DispatchQueue.main.async(execute: {() -> Void in
            self.presentingViewController?.dismiss(animated: true, completion: {() -> Void in
                self.tmpWindow?.windowLevel = UIWindow.Level(rawValue: 0.0)
                UIApplication.shared.delegate?.window??.makeKeyAndVisible()
                if let statusBarStyle = self.previousStatusBarStyle {
                    self.setNeedsStatusBarAppearanceUpdate()
                }
            })
        })
    }
    

    // In your view controller's scope
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return previousStatusBarStyle ?? .default
    }

}
于 2020-11-01T09:50:21.613 回答