0

So i have a viewcontroller which is basically an alert window which is supposed to be a popup and be dismissed by the tap on outside its frame.

But whenever i call that VC, it is always displayed as fullscreen and not as a pop up window.

I have tried a couple of ways to do this, namely as mentioned below.

 if let exp : String = expiredVehicles[i] {
        expiredVehicleNumber = expiredVehicles[i]
            let popUpVC = SubscriptionExpired()
            popUpVC.modalTransitionStyle = .crossDissolve
            popUpVC.modalPresentationStyle = .popover // also tried other presentation styles but none work and it is still fullscreen
            popUpVC.view.backgroundColor = UIColor.white.withAlphaComponent(0.8)
            self.present(popUpVC, animated: true, completion: nil)
        }

in case anyone need to see the definition of that VC, i will be glad to share it

i feel i should mention that the VC to be displayed as a popup is inheriting UIViewController

Any insight that might help would be great.

Thanks for the input

vgvishesh23113
  • 309
  • 3
  • 12

3 Answers3

0

One potential way is to add a tap gesture recognizer to your View, which dismisses the VC.

But this will only be helpful if this popup has read-only info and doesn't require any further action from the user.

func addTapRecognizer(){
    let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap))
    self.view.addGestureRecognizer(tap)
}

@objc func handleTap(){
       // dismiss the VC here
    }
}
nishkaush
  • 1,512
  • 1
  • 13
  • 20
  • yes i am aware of this solution but this is more like a workaround and not sure if my employer will allow it. also the problem is that VC has a button as well which will redirect to customer helpline page so if i add tap gesture to the whole view, that button will stop working – vgvishesh23113 May 17 '19 at 06:59
0

You can call following method to show popup:

    let popupVC = SubscriptionExpired()
    popupVC.modalPresentationStyle = .overCurrentContext
    self.addChild(popupVC)
    popupVC.view.frame = self.view.frame
    self.view.addSubview(popupVC.view)
    popupVC.didMove(toParent: self)
}

Then, for removing that popup you can use:

UIView.animate(withDuration: 0.25, animations: {
    self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
    self.view.alpha = 0.0
}, completion: { (finished) in
    if finished {
        self.view.removeFromSuperview()
    }
})

In that case I have a button inside popup and whenever that button pressed above method triggers. Can you please try it? I can edit my answer according to your needs.

atalayasa
  • 3,310
  • 25
  • 42
  • brother, everything in this app is done completely programatically, no storyboard or interface builder is being used anywhere, so i dont think this will be of any help....or would it? – vgvishesh23113 May 17 '19 at 07:08
  • It's okay even if your app is done programmatically, you can use these methods like `let popupVC = PopupVC()` and show that view controller. – atalayasa May 17 '19 at 07:16
  • ok i will give it a shot, but where should i declare the type of my VC to be presented. keep in my mind i am still an amateur at swift – vgvishesh23113 May 17 '19 at 07:19
  • no luck either brother, now the VC is fullscreen but the UITabbar is visible but still dont go away, when i add the second part of the code to dismiss, it just turn whole view to black screen but uitabbar still visible – vgvishesh23113 May 17 '19 at 07:33
0

You need to implement this in you presenting view controller:

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    // Return no adaptive presentation style, use default presentation behaviour
    return .none
}

UIPopoverPresentationController displaying popover as full screen

Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85