Popover in swift 3 on iphone ios
Asked Answered
W

1

32

I am trying to make a popover menu with the following code:

import UIKit

class BeobachtungViewController: UIViewController, UIPopoverPresentationControllerDelegate {


    @IBAction func addClicked(_ sender: AnyObject) {
        // get a reference to the view controller for the popover
        let popController = UIStoryboard(name: "Personenakte", bundle: nil).instantiateViewController(withIdentifier: "popoverId")

        // set the presentation style
        popController.modalPresentationStyle = UIModalPresentationStyle.popover

        // set up the popover presentation controller
        popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
        popController.popoverPresentationController?.delegate = self
        popController.popoverPresentationController?.sourceView = sender as! UIView // button
        popController.popoverPresentationController?.sourceRect = sender.bounds

        // present the popover
        self.present(popController, animated: true, completion: nil)
    }

    // UIPopoverPresentationControllerDelegate method
    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        // Force popover style
        return UIModalPresentationStyle.none
    }
}

This is working on iPad, but, on an iPhone, the popup takes the whole iPhone screen. I just want a small window with an arrow. I found several tutorials but none worked for me.

Wakayama answered 11/10, 2016 at 8:12 Comment(2)
as far as i know, that is the intended behaviour by apple, there are no popups on iphoneAnthiathia
your code it's working for me.... but your delegate it's not correctDodge
D
60

Change your delegate method to:

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    // return UIModalPresentationStyle.FullScreen
    return UIModalPresentationStyle.none
}
Dodge answered 11/10, 2016 at 10:32 Comment(4)
@Dodge Not working in iPhone, please let me know how does it work?Zoologist
What if you only want one of the controller to be popover not all? I have a situation that I only want one of the controllers to be popover.Mena
@LimThyeChean The delegate method hands you the controller. You could guard for that one (because you've kept a reference to it) and return accordingly.Cainozoic
It seems like this does not work with a TableViewController as PopoverBlackcock

© 2022 - 2024 — McMap. All rights reserved.