从程序化创建按钮在iPhone(Swift 3)上弹出

时间:2017-07-05 03:17:45

标签: ios iphone swift swift3

当我点击以编程方式创建的按钮时,我试图在iPhone上显示一个弹出框。但是,我发现popover占据了整个屏幕。到目前为止,我已经在popover视图控制器的类中编写了以下代码:

import UIKit

class GroupSetsPopoverController: UIViewController, UIPopoverPresentationControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()

    self.preferredContentSize = CGSize(width: 375, height: 162)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "infoButtonSegue" {
        let popoverViewController = segue.destination as! GroupSetsPopoverController

        let pvc = popoverViewController.popoverPresentationController
        pvc?.delegate = self

        //popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
        //popoverViewController.popoverPresentationController!.delegate = self
    }
}

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

任何指导都将不胜感激。我试图在Stack Overflow上关注各种教程和帖子,但无济于事。

1 个答案:

答案 0 :(得分:0)

您可以查看answerlibrary以获取参考资料。如果GroupSetsPopoverController是你的popover viewcontroller,你可以这样做:

init(for sender: UIView)) {
    super.init(nibName: nil, bundle: nil)

    modalPresentationStyle = .popover
    guard let pop = popoverPresentationController else { return }
    pop.sourceView = sender
    pop.sourceRect = sender.bounds
    pop.delegate = self
}