Reputation: 403
Popover takes the complete screen when displayed in landscape mode, it works correctly in portrait mode though. Also, it is does not disappear when i click outside the popover in landscape mode.
I connected the popover through the storyboard. Inside the popoverviewcontroller I placed a view which contains the buttons. The code for the viewdidload() of the popoverviewcontroller is:
override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = popoverView.frame.size
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Portrait:
landscape:
Upvotes: 9
Views: 1781
Reputation: 276
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.
Upvotes: 15
Reputation: 536
You have to add UIPopoverPresentationControllerDelegate
to 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.
Upvotes: 14