Swift 3中iPhone iOS的弹出窗口

32

我正在尝试使用以下代码制作一个弹出式菜单:

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

这在iPad上运行正常,但是在iPhone上,弹出窗口占据了整个iPhone屏幕。我只想要一个带箭头的小窗口。我找到了几个教程,但都不适用于我。


据我所知,这是苹果的预期行为,在iPhone上没有弹出窗口。 - Fonix
1
你的代码对我来说是有效的... 但是你的委托不正确。 - TonyMkenu
1个回答

60

将您的委托方法更改为:

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

1
@TonyMkenu 在iPhone上无法工作,请告诉我它如何工作? - Gopal Devra
如果您只想让一个控制器成为弹出视图而不是所有控制器,该怎么办?我有这样一种情况,我只想让其中一个控制器成为弹出视图。 - Lim Thye Chean
1
@LimThyeChean 委托方法将控制器交给您。您可以对其进行保护(因为您已经保留了对它的引用),并相应地返回。 - Murray Sagal
似乎在TableViewController作为Popover时,这个功能无法实现。 - ndreisg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接