swift - Popover is not displayed correctly in landscape mode

The answer from @Jake2Finn works for Swift 4.0.

The trait parameter specifically is required to fix the landscape problem:

traitCollection: UITraitCollection

Without it the function adaptive... only works for portrait.


You have to add UIPopoverPresentationControllerDelegateto your class like this:

swift 3

import UIKit

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    ...

As a second step, add the following function:

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

Explanation: By returning UIModalPresentationStyle as none, the original presentation style is kept and your popover is not streched to the bottom of your screen in landscape orientation.

Tags:

Ios

Swift

Popover