UIViewController In-Call Status Bar Issue

前端 未结 8 917
误落风尘
误落风尘 2020-12-01 12:24

Issue:

Modally presented view controller does not move back up after in-call status bar disappears, leaving 20px empty/transparent space at the top.<

相关标签:
8条回答
  • 2020-12-01 13:26

    I had the same issue with the personnal hospot modifying the status bar. The solution is to register to the system notification for the change of status bar frame, this will allow you to update your layout and should fix any layout issue you might have. My solution which should work exactly the same for you is this :

    • In your view controller, in viewWillAppear suscribe to the UIApplicationDidChangeStatusBarFrameNotification

          NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(myControllerName.handleFrameResize(_:)), name: UIApplicationDidChangeStatusBarFrameNotification, object: nil)
      
    • Create your selector method

      func handleFrameResize(notification: NSNotification) {
      self.view.layoutIfNeeded() }
      
    • Remove your controller from notification center in viewWillDisappear

          NSNotificationCenter.defaultCenter().removeObserver(self, name: UIApplicationDidChangeStatusBarFrameNotification, object: nil)
      
    • You also need your modal to be in charge of the status bar so you should set

      destVC.modalPresentationCapturesStatusBarAppearance = true before presenting the view.

    You can either implement this on every controller susceptible to have a change on the status bar, or you could make another class which will do it for every controller, like passing self to a method, keep the reference to change the layout and have a method to remove self. You know, in order to reuse code.

    0 讨论(0)
  • 2020-12-01 13:29

    In my case, I'm using custom presentation style for my ViewController. The problem is that the Y position is not calculated well. Let's say the original screen height is 736p. Try printing the view.frame.origin.y and view.frame.height, you'll find that the height is 716p and the y is 20. But the display height is 736 - 20(in-call status bar extra height) - 20(y position). That is why our view is cut from the bottom of the ViewController and why there's a 20p margin to the top. But if you go back to see the navigation controller's frame value. You'll find that no matter the in-call status bar is showing or not, the y position is always 0.

    So, all we have to do is to set the y position to zero.

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        let f = self.view.frame
    
        if f.origin.y != 0 {
            self.view.frame = CGRect(x: f.origin.x, y: 0, width: f.width, height: f.height)
            self.view.layoutIfNeeded()
            self.view.updateConstraintsIfNeeded()
        }
    }
    
    0 讨论(0)
提交回复
热议问题