显示从iPhone导航栏中的栏按钮弹出

时间:2016-12-27 05:05:55

标签: ios iphone swift uibarbuttonitem uipopover

在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 个答案:

答案 0 :(得分:6)

这里唯一的问题是你在设置其弹出代理之前提交OptionsViewController。因此,首先设置其委托,然后调用当前函数。

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

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