5

I am having issues using a View Controller modally for a sign in page. I can get the controller to appear but I can not change it from full size.

I am trying to present the popover in the center of the screen with a faded background. The popover should dismiss when I click outside of the view.

I have looked through the questions and answers throughout the site and have not found one that has worked for me.

Here is the code I have:

import UIKit

class SignInView: UIViewController, UIPopoverPresentationControllerDelegate {


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "popoverSegue" {

    var popover = segue.destination as! SignInPopView
    popover.popoverPresentationController!.delegate = self
    popover.modalPresentationStyle = UIModalPresentationStyle.popover
    popover.preferredContentSize = CGSize(width: 375, height: 500)

    }

}
JSharpp
  • 459
  • 1
  • 9
  • 21

3 Answers3

3

To make your view controller shown as a popup, you should set the following:

popupVC.modalPresentationStyle = .OverCurrentContext

popupVC.modalTransitionStyle = .CrossDissolve

And also design your view controller's position, size to make it look like a popup. Also you can use EzPopup thats a nice pod I use EzPopup and it worked for me

Community
  • 1
  • 1
0

This answer has worked for me: https://stackoverflow.com/a/46518387/640588

class CommonViewController: UIViewController, UIPopoverPresentationControllerDelegate{

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

func showPopover(){
    let storyboard = UIStoryboard(name: "Pickers", bundle: nil)
    let myViewController = UIViewController()
    myViewController.preferredContentSize = CGSize(width: 320, height: 200)
    myViewController.modalPresentationStyle = .popover

    let popOver = myViewController.popoverPresentationController
    popOver?.delegate = self

    self.present(myViewController, animated: true, completion: nil)
    popOver?.permittedArrowDirections = .init(rawValue: 0)
    popOver?.sourceView = self.view

    let rect = CGRect(
        origin: CGPoint(x: self.view.frame.width/2, y: self.view.frame.height/2),
        size: CGSize(width: 1, height: 1)
    )
    popOver?.sourceRect = rect
}
Genki
  • 3,055
  • 2
  • 29
  • 42
0

My solution was to create a modal segue to the new view controller. Place the content in a view within the view controller and set the view controllers background opacity to the desired amount. This can all be done through storyboard.

This way when the popover view controller appears the view appears where I want it, in the correct size, and the previous view controller is still visible behind the background.

The only issue I'm having with this is dismissing the modal popover when I click outside of the view.

This video was helpful: https://www.youtube.com/watch?v=S5i8n_bqblE

Edit: This Detect any tap outside the current view provided the solution to dismissing the modal when you touch outside of the view.

JSharpp
  • 459
  • 1
  • 9
  • 21
  • But that is not a popover. – matt Nov 25 '18 at 06:10
  • I suppose it's more of a modal or popup than a popover with the arrow. Either way it was exactly what I was looking for. I updated the answer with how to dismiss the modal when you touch outside the view. – JSharpp Nov 26 '18 at 04:05
  • But if that is what you were looking for, calling it a popover was unfair and misleading. – matt Nov 26 '18 at 04:53
  • I agree, it was an honest mistake since they are very similar. I will change the title of the question to prevent confusion. This is a fully functional solution to the general problem either way. – JSharpp Nov 26 '18 at 04:56