在App Store这样的通话中,如何将状态栏高度设置为20pt?

时间:2018-03-09 15:33:15

标签: ios statusbar

在iOS App Store中打电话时,状态栏在阅读文章时从40pt变为20pt。如何在我的应用中执行此操作?enter image description here

1 个答案:

答案 0 :(得分:2)

这真是一个有趣的问题,有时带我去弄明白。

  1. 如果你注意到这样的viewcontroller中的app store没有状态栏,这意味着绿色栏根本不应该显示。这是第一个提示。

  2. 您可以通过UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow

  3. 访问状态栏
  4. 根据第一点,这意味着状态栏没有被隐藏,但实际上它的statusBarWindow.frame.origin.y刚刚向上移动。

  5. 请不要单独处理iPhone x

  6. 请确保状态栏未隐藏

  7. 请注意,这不是唯一正确的方法,现在您拥有视图本身,您可以更改原点或大小,甚至尝试获取此视图中的内容并隐藏它们或更改其帧等。

  8. 这是一个如何做到的例子。

    class ViewController: UIViewController {
    
    var isHidden:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIView.animate(withDuration: 0.5) { () -> Void in
            let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
            statusBarWindow.frame = CGRect(x: 0, y: -20, width: statusBarWindow.frame.size.width, height: 40.0)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override var prefersStatusBarHidden: Bool{
        return isHidden
    }
    

    }

    还附上了一张包含此代码结果的图片,希望这个答案对你也有用。

    enter image description here