【问题标题】:Popover in swift 3 on iphone iosiphone ios上swift 3中的弹出框
【发布时间】:2017-03-14 02:34:43
【问题描述】:

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

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上没有弹出窗口
  • 你的代码对我有用....但是你的委托不正确

标签: ios iphone swift3 popover


【解决方案1】:

将您的委托方法更改为:

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

【讨论】:

  • @TonyMkenu 不能在 iPhone 上工作,请告诉我它是如何工作的?
  • 如果您只希望其中一个控制器弹出而不是全部怎么办?我有一种情况,我只希望其中一个控制器弹出。
  • @LimThyeChean 委托方法将控制器交给您。您可以保护那个(因为您保留了对它的引用)并相应地返回。
  • 这似乎不适用于作为 Popover 的 TableViewController
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多