0
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

sweta.me
  • 273
  • 1
  • 11
  • 1
    Any crash reports? – Ravi Feb 21 '17 at 06:14
  • In console, none. Except: `libc++abi.dylib: terminating with uncaught exception of type NSException` – sweta.me Feb 21 '17 at 06:15
  • 1
    show teh full crash report – Anbu.Karthik Feb 21 '17 at 06:20
  • put logs in SomeVC's view life cycle methods and check whether that view presenting or not and try commenting the line 'obj.popoverPresentationController?.sourceRect' – Ravi Feb 21 '17 at 06:20
  • @Anbu.Karthik Not able to get anything related to crash except the above mentioned exception. Exception break point is also set. But, it's not letting me know on which line it's crashing. – sweta.me Feb 21 '17 at 06:23
  • then enable the zombies and check once – Anbu.Karthik Feb 21 '17 at 06:24
  • @raki `viewDidLoad` got printed in console. `viewWillAppear` didn't. – sweta.me Feb 21 '17 at 06:27
  • @Anbu.Karthik Enabled zombies. How to trace now? – sweta.me Feb 21 '17 at 06:30
  • @sweta.me - run once it automtcailly thorw the exception if it is crash – Anbu.Karthik Feb 21 '17 at 06:31
  • @Anbu.Karthik same exception. No additional information. `libc++abi.dylib: terminating with uncaught exception of type NSException` – sweta.me Feb 21 '17 at 06:33
  • @sweta.me - really amazing can you attach ur project – Anbu.Karthik Feb 21 '17 at 06:37
  • @sweta.me so something is wrong in your SomeVC, once check the code by putting break points in that class starting from viewDidLoad. – Ravi Feb 21 '17 at 06:44
  • @Anbu.Karthik Really, can't. Quite large project and confidential, too. – sweta.me Feb 21 '17 at 06:50
  • Just to narrow down the problem if it is with your ViewController or presenting a popover, can you please try presenting UIAlertView/UIAlertViewController at the same point you are presenting pop over ? – NeverHopeless Feb 21 '17 at 07:16
  • Just changing `sourceRect` to `sourceView` like: `obj.popoverPresentationController?.sourceView = self.view` makes it run fine. But, I want the pop-over to appear in center – sweta.me Feb 21 '17 at 08:08
  • What if you write this? `obj.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: UIScreen.main.bounds.size.height - self.view.bounds.midY, width: 1, height: 1)` – NeverHopeless Feb 21 '17 at 09:45

3 Answers3

2

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.

sweta.me
  • 273
  • 1
  • 11
1
Try this:
    let obj = MainStoryboard().instantiateViewController(withIdentifier: "SomeVC") as! SomeVC
    obj.delegate = self
    obj.modalPresentationStyle = .overCurrentContext
    self.navigationController?.present(obj, animated: false, completion: {
                            })
Anuraj
  • 1,242
  • 10
  • 25
  • This is not crashing but, the pop over is fullscreen and I want it to be 682 x 450 in size. So, kind of not working. – sweta.me Feb 21 '17 at 08:12
  • I don't know whether it is possible. Try changing the constraints of objViewcontroller so that it is presented to the required dimension. – Anuraj Feb 21 '17 at 08:34
1

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.

viral
  • 4,168
  • 5
  • 43
  • 68