ios - 在风景中呈现模态 VC 时,父 VC 的安全区域发生变化

标签 ios ios11 landscape landscape-portrait safearealayoutguide

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

GIF with described bug

GIF with described bug that touches TabBar and TableView

第一 View Controller :

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)
  }
}

第二个 View Controller :

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

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

控制台输出:

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

最佳答案

这就是我解决约束问题的方法: 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?()
    })
  }

关于ios - 在风景中呈现模态 VC 时,父 VC 的安全区域发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47223680/

相关文章:

swift - 如何在 Swift 中检测横向方向并将其应用于 AVCaptureVideoPreviewLayer?

ios - NSCalendar currentCalendar setTimeZone 未设置

ios - 在swift中使用struct创建数组

ios - UIColor(命名为 :) always returns nil on iOS 11. 0-11.2

html - iOS 11,Cordova 应用侧边栏在滚动时消失

iphone - iPhone 的 UISplitViewController

ios - 如何在 iOS 13 的 NFCTagReaderSession 中读取不同类型的 NFC 标签?

iphone - CALayer 阴影和半透明 UINavigationBars

ios - xcode 9 中的 IBanimitable 问题?

iOS6 方向