在iPhone中,从导航栏中的工具栏按钮弹出弹出窗口

6

在Swift中,我试图从导航栏顶部右侧位置的一个条形按钮显示一个弹出窗口。以下是我的代码:

func showOptions(sender: UIBarButtonItem) {
    let optionsVC = OptionsViewController(nibName: "OptionsViewController", bundle: nil)
    optionsVC.delegate = self
    optionsVC.modalPresentationStyle = .popover
    optionsVC.preferredContentSize = CGSize(width: 200, height: 200)

    present(optionsVC, animated: true, completion: nil)

    let popController = optionsVC.popoverPresentationController
    popController?.permittedArrowDirections = .up
    popController?.delegate = self
    popController?.barButtonItem = sender
}

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

iPad上运行良好,但在iPhone上却无法正常工作。我已经查阅了文档和不同的网页,一切似乎都是正确的。我的代码缺少什么?

1个回答

6
这里唯一的问题就是在设置OptionsViewController的弹出代理之前,你就进行了呈现。所以请先设置代理再调用呈现函数。
let popController = optionsVC.popoverPresentationController
popController?.permittedArrowDirections = .up
popController?.delegate = self
popController?.barButtonItem = sender

present(optionsVC, animated: true, completion: nil)

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