iosswiftswift3uipopoveruimodalpresentationstyle

Swift3: Crash on presenting pop over


let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
obj.delegate = self

obj.modalPresentationStyle = .popover
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
self.present(obj, animated: true, completion: nil)

On setting up breakpoints, debugger goes good till last line. After that, it directly goes to AppDelegate class first line.

I have set exception break point properly. Where I might be making a mistake? Is it related to sourceView for popoverPresentationController? I am not sure.

What I want to do is set up the popoverPresentationController in center. Any help?

EDIT: I added the sourceView to the code like following & now it's working:

obj.popoverPresentationController?.sourceView = self.view
obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)

However, it's not in the center of the screen. Putting screen shot for reference. How do I make it to the center and remove the direction arrow?

enter image description here


Solution

  • You have to use sourceView in conjunction with sourceRect to provide anchor point for pop over, like following:

    obj.popoverPresentationController?.sourceView = self.view
    obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 1, height: 1)
    

    Also, If you don't want the anchor point arrow to be there, then use:

    obj.popoverPresentationController?.permittedArrowDirections = .init(rawValue: 0)
    

    It will make your pop over appear in center with no arrow/anchor point.