JSharpp
JSharpp

Reputation: 559

Presenting a View Controller Modally

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)

    }

}

Upvotes: 5

Views: 8830

Answers (3)

JSharpp
JSharpp

Reputation: 559

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.

Upvotes: 0

alireza kiani
alireza kiani

Reputation: 41

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

Upvotes: 3

Genki
Genki

Reputation: 3195

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
}

Upvotes: 0

Related Questions