swift - 弹出窗口在横向模式下显示不正确

swift - Popover is not displayed correctly in landscape mode

Popover 在横向模式下显示时占据整个屏幕,但在纵向模式下它可以正常工作。此外,当我在横向模式下单击弹出窗口外时,它不会消失。

我通过故事板连接了弹出窗口。在 popoverviewcontroller 中,我放置了一个包含按钮的视图。 popoverviewcontroller的viewdidload()的代码是:

override func viewDidLoad() {
    super.viewDidLoad()

    self.preferredContentSize = popoverView.frame.size
        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

纵向:

横向:

您必须像这样将 UIPopoverPresentationControllerDelegate 添加到您的 class 中:

swift 3

import UIKit

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    ...

作为第二步,添加以下函数:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

解释: 通过将 UIModalPresentationStyle 返回为 none,保留了原始的呈现样式,并且您的弹出窗口不会横向拉伸到屏幕底部。

@Jake2Finn 的回答适用于 Swift 4.0.

特别需要特征参数来修复景观问题:

traitCollection: UITraitCollection

没有它自适应功能...仅适用于肖像。