在风景中呈现模态VC时,父VC中的安全区域更改

fil*_*ish 11 landscape ios landscape-portrait ios11 safearealayoutguide

当我呈现仅支持横向的SecondViewController时,第一个UIViewController中的安全区域插入会更改为安全区域插图.

GIF描述了bug

GIF描述了触及TabBar和TableView的bug

FirstViewController:

class ViewController: UIViewController {

  @IBAction func showSecondVC(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "SecondViewController")

    self.present(controller, animated: true, completion: {
      print("completed")
    })
  }

  override func viewSafeAreaInsetsDidChange() {
    print(view.safeAreaInsets)
    super.viewSafeAreaInsetsDidChange()
    print(view.safeAreaInsets)
  }
}
Run Code Online (Sandbox Code Playgroud)

第二个ViewController:

class SecondViewController: UIViewController {
  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return [.landscape]
  }

  @IBAction func dismissYourSelff(_ sender: Any) {
    self.dismiss(animated: true, completion: nil)
  }
}
Run Code Online (Sandbox Code Playgroud)

控制台输出:

UIEdgeInsets(top: 44.0, left: 0.0, bottom: 34.0, right: 0.0)
UIEdgeInsets(top: 44.0, left: 0.0, bottom: 34.0, right: 0.0)
UIEdgeInsets(top: 0.0, left: 44.0, bottom: 21.0, right: 44.0)
UIEdgeInsets(top: 0.0, left: 44.0, bottom: 21.0, right: 44.0)
completed
Run Code Online (Sandbox Code Playgroud)

fil*_*ish 2

这就是我通过约束解决问题的方法: https://gist.github.com/filletofish/b56600e9e661aa6e49cc0a56a98b37a3

我必须使用快照来防止 TabBar 和 TableView 的错误布局。

override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
    var completionToRemoveSnapshot: (() -> ())? = nil

    let shouldUseSnapshot = SDiOSVersion.deviceSize() == .Screen5Dot8inch &&
      viewControllerToPresent.supportedInterfaceOrientations == .landscape

    if shouldUseSnapshot {
      if let snapShot = self.view.snapshotView(afterScreenUpdates: true) {
        viewController.view.addSubview(snapShot)
        completionToRemoveSnapshot = { snapShot.removeFromSuperview() }
      }
    }

    super.present(viewControllerToPresent, animated: flag, completion: {
      completionToRemoveSnapshot?()
      completion?()
    })
  }
Run Code Online (Sandbox Code Playgroud)