Oliver Schäfer
Oliver Schäfer

Reputation: 11

UIPopoverPresentationController Swift

I’m using Storyboards and AutoLayout. There are 2 scenes, 1 is my main view controller with a toolbar and several UIBarButtonItems and 1 is a UIViewController that should simply display a UILabel. When the user taps a toolbar in my main view controller, I present the popover with the following code:

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)
}

The view gets presented but no subviews (UILabel) are visible. The same strange behaviour happens, when I setup storyboard segues instead of doing the popover presentation from code. However, when I push the very same view on my UINavigationController everything gets displayed fine. Am I missing something?

Best Regards, Oliver

Upvotes: 1

Views: 10233

Answers (2)

Harshil Kotecha
Harshil Kotecha

Reputation: 2896

For best tutorial click here

  • create one XIB and swift file for popup menu . (ex. MenuViewController)
  • create another xib or storyboard view controller file for display popup menu . (ex. ViewController)
  • in viewController file add UIPopoverPresentationControllerDelegate
  • add button in ViewController and create IBAction and write bellow code.

@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)
        }
    }

For best tutorial click here

Upvotes: 0

Gene De Lisa
Gene De Lisa

Reputation: 3838

Adjust the sizes to what you want. You can set the barbutton instead of the sourceView if you pass that in.

    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)
        }
    }

Upvotes: 1

Related Questions