sweta.me
sweta.me

Reputation: 273

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

Upvotes: 0

Views: 630

Answers (3)

sweta.me
sweta.me

Reputation: 273

After doing the following code changes I was able to make it work.

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

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

Thank You all for your efforts.

Upvotes: 2

viral
viral

Reputation: 4208

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.

Upvotes: 1

Anuraj
Anuraj

Reputation: 1240

Try this:
    let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
    obj.delegate = self
    obj.modalPresentationStyle = .overCurrentContext
    self.navigationController?.present(obj, animated: false, completion: {
                            })

Upvotes: 1

Related Questions