Niroj
Niroj

Reputation: 1114

PopOver view controller covers full screen

This is my code for PopOver. I have two View controllers. I am presenting the PopOver in Messaging View controller and the view controller that need to be piped is PreferencesView controller. Storyboard Id is also same Preferences View controller. The popOver is success but always covers the full screen. Even though UIModalPresentationStyle.None. What am I doing wrong here?

 func optionClicked(sender:UIBarButtonItem)
{
    print(")show set preference and set reminder option")


    let preferenceAction: UIAlertAction = UIAlertAction(title: "Set preferences", style: .Default) { action -> Void in
        self.optionChoosed(true)
        }
    let reminderAction: UIAlertAction = UIAlertAction(title: "Set reminder", style: .Default) { action -> Void in
        self.optionChoosed(false)
    }
    let actionSheetController: UIAlertController = UIAlertController(title: kAlertTitle, message: "What you want to do?", preferredStyle: .ActionSheet)

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel) { action -> Void in
    }
    actionSheetController.addAction(preferenceAction)
    actionSheetController.addAction(reminderAction)
    actionSheetController.addAction(cancelAction)
    self.presentViewController(actionSheetController, animated: true, completion: nil)
}

func optionChoosed(isSetPreference:Bool)
{


    if(isSetPreference)
    {
    print("Set preference")

        let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController
        vc.modalPresentationStyle = UIModalPresentationStyle.Popover
        let popover: UIPopoverPresentationController = vc.popoverPresentationController!
        popover.barButtonItem?.action = "isSetPreference"
        popover.delegate = self
        presentViewController(vc, animated: true, completion:nil)


    }

    func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.None

    }
    func dismiss() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

Upvotes: 1

Views: 1654

Answers (3)

Emilio Hoffmann
Emilio Hoffmann

Reputation: 304

Implement this function where you call the popover

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

Upvotes: 2

Sahil
Sahil

Reputation: 9226

I guess you are performing segue from storyboard. if you are doing so remove segue from storyboard and create an action for bar button item and put this code there.

    let storyboard : UIStoryboard = UIStoryboard(name: "Messaging", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("PreferencesViewController") as! PreferencesViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.Popover
    let popover: UIPopoverPresentationController = vc.popoverPresentationController!
    vc.preferredContentSize = CGSize(width: 200, height: 200)
    popover.barButtonItem = sender as? UIBarButtonItem
    popover.delegate = self
    presentViewController(vc, animated: true, completion:nil)

Upvotes: 3

aahmetbas
aahmetbas

Reputation: 64

Try to add

vc.preferredContentSize = CGSize(width: 200, height: 200)

I hope it will help

Upvotes: 0

Related Questions