UIBarButtonItem为popoverPresentationController提供了错误的框架

时间:2016-08-02 17:11:49

标签: ios swift uibarbuttonitem uipopover

我的工具栏左侧有UIBarButtomItem,标签为Admin

enter image description here

我需要在点击它时显示一个弹出框,但它出现在错误的位置,它出现在屏幕的左上角:

enter image description here

这是打开popover的功能:

// this IBAction is assigned only to the Admin bar button item
@IBAction func openAdmin(sender: UIBarButtonItem) {
    let alertController = UIAlertController(title: nil, message: "Elige una opción", preferredStyle: .ActionSheet)

    // action button initializations... ... ...

    let barButtonItemSize = sender.valueForKey("view")

    alertController.popoverPresentationController?.sourceView = self.view
    alertController.popoverPresentationController?.sourceRect = barButtonItemSize!.frame

    presentViewController(alertController, animated: true, completion: nil)
}

当我触摸管理栏按钮时,我也会在控制台上收到此警告:

  

快照未呈现的视图会导致空快照。确保您的视图在屏幕更新后的快照或快照之前至少呈现一次

我做错了什么?或者我需要做什么才能在管理员的顶部显示popover?

1 个答案:

答案 0 :(得分:5)

替换

alertController.popoverPresentationController?.sourceView = self.view
alertController.popoverPresentationController?.sourceRect = barButtonItemSize!.frame

alertController.popoverPresentationController?.sourceView = sender
alertController.popoverPresentationController?.barButtonItem = sender