我试图用UIPopoverPresentationController来显示一个不占整个屏幕的popover.我已经学习了很多不同的教程,但都不走运.

这是我的密码.它正确地实例化了ViewController,但它占用了整个屏幕,而不是我在preferredContentSize中定义的更小的屏幕.

func showPopover() {
    let vc = self.storyboard?.instantiateViewControllerWithIdentifier("PopupTimePickerViewController") as PopupTimePickerViewController
    vc.modalPresentationStyle = .Popover
    vc.preferredContentSize = CGSizeMake(200, 100)

    if let presentationController = vc.popoverPresentationController {
        presentationController.delegate = self
        presentationController.permittedArrowDirections = .Up
        presentationController.sourceView = self.view
        presentationController.sourceRect = CGRectMake(0, 0, 50, 50)

        self.presentViewController(vc, animated: true, completion: nil)
    }
}

Update 9/27/16 with correct answer

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

推荐答案

在iPhone中,您应该添加以下内容以呈现popover.

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle {
    // Return no adaptive presentation style, use default presentation behaviour
    return .None
}

Swift相关问答推荐

如何在SwiftUI中创建具有圆角顶部和锯齿状底部边缘的自定义Shape,类似于撕破的纸?

TabView中的视频播放器意外播放

如何禁用MacOS上SwiftUI中按钮周围的聚焦环(或高亮显示)?

我正在try 通过代码设置节中页眉和页脚的高度,但它不起作用

使用MKLocalSearch获取坐标

SWIFT`String.Encoding`:`.unicode`vs`.utf16`

如何获取嵌套数组中项的路径以修改数组?

如何写一个;风格;视图修饰符,它会影响特定类型的所有嵌套视图?

如何在 Vapor 中制作可选的查询过滤器

Observable.create 捕获行为

如何使用 Swift 在非本地函数中切换()一个 Bool?

签署GoogleSignIn-GoogleSignIn需要开发团队

将 struct 作为泛型类型传递并访问该泛型类型属性

Vapor - 流利的,将对象保存到 PostgreSQL

在 iOS 中使用 Swift 保存 PDF 文件并显示它们

我可以在 Swift 中为泛型类型 T 分配默认类型吗?

即使设置为从不也可以访问 iOS11 照片库

如何使用 swift 将 Images.xcassets 中的图像加载到 UIImage 中

如何从一个可观察的数组创建一个可观察的数组?

如何交换快速数组中的元素?