UIPopoverPresentationController Swift

时间:2014-09-04 15:42:34

标签: ios swift uipopovercontroller

我正在使用Storyboards和AutoLayout。有2个场景,1个是我的主视图控制器,带有工具栏和几个UIBarButtonItems,1个是UIViewController,应该只显示UILabel。当用户点击我的主视图控制器中的工具栏时,我使用以下代码显示弹出窗口:

func presentPopover(viewCtrl: UIViewController, item: UIBarButtonItem) {
viewCtrl.modalPresentationStyle = .Popover
let popover: UIPopoverPresentationController = viewCtrl.popoverPresentationController!
popover.permittedArrowDirections = .Any
popover.barButtonItem = item
self.presentViewController(viewCtrl, animated: true, completion: nil)
}

helpView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("HelpView“) as? HelpVC
if helpView != nil {
    presentPopover(helpView!, helpButton)
}

显示视图但没有可见的子视图(UILabel)。当我设置故事板segues而不是从代码执行popover演示时,会发生同样奇怪的行为。但是,当我在UINavigationController上推送相同的视图时,一切都显示得很好。我错过了什么吗?

最诚挚的问候, 奥利弗

2 个答案:

答案 0 :(得分:1)

根据需要调整尺寸。 如果将其传入,则可以设置barbutton而不是sourceView。

    if let controller = viewCtrl {
        controller.preferredContentSize = CGSizeMake(200,25)
        controller.modalPresentationStyle = UIModalPresentationStyle.Popover
        if let popover = controller.popoverPresentationController  {
            popover.delegate = self
            popover.sourceView = viewCtrl.view
            popover.sourceRect = CGRectMake(0,-5, 50, 50)
            popover.permittedArrowDirections = .Any
            self.presentViewController(controller, animated: true, completion: nil)
        }
    }

答案 1 :(得分:1)

获取最佳教程click here

  • 为弹出菜单创建一个XIB和swift文件。 (例如MenuViewController)
  • 为显示弹出菜单创建另一个xib或storyboard视图控制器文件。 (例如ViewController)
  • 在viewController文件中添加UIPopoverPresentationControllerDelegate
  • 在ViewController中添加按钮并创建IBAction并编写以下代码。
@IBAction func onClickMenu(_ sender: UIButton)
    {
        if btnMenu.isSelected
        {
            btnMenu.setImage(UIImage(named: "arrow_orange"), for: UIControlState.normal)

            btnMenu.isSelected = false

            self.dismiss(animated: true, completion: nil)
        }
        else
        {
            btnMenu.setImage(UIImage(named: "up_arrow_orange"), for: UIControlState.normal)

            btnMenu.isSelected = true

            let popMenu = MenuViewController(nibName: "MenuViewController", bundle: nil)
            popMenu.modalPresentationStyle = UIModalPresentationStyle.popover

            popMenu.preferredContentSize = CGSize.init(width: 320, height: 265)

            popMenu.vcPush = self

            self.present(popMenu, animated: true, completion: nil)

            let popController :  UIPopoverPresentationController = popMenu.popoverPresentationController!

            popController.permittedArrowDirections = UIPopoverArrowDirection.up

            popController.delegate = self

            //popController?.barButtonItem = nil

            popController.sourceView = self.view

            popController.sourceRect = CGRect.init(x: 525, y: 30, width: 10, height: 10)
        }
    }

获取最佳教程click here