Niroj thapa Niroj thapa - 10 days ago 6
iOS Question

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

Answer

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