black screen visible when dragging viewcontroller to tabbarcontroller in swift?

351 Views Asked by At

When i am presenting from TabbarController to Outside Viewcontroller presenting correctly and also Dismiss correctly.but when i drag to dismiss the ViewController it show's Black screen using UIPanGestureRecognizer.

From TabbarController to ViewController Present Code

  let newVC = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
  self.definesPresentationContext = true
  newVC.modalPresentationStyle = .overCurrentContext
  self.present(newVC, animated: true, completion: nil)

Dismiss ExampleViewController to TabbarController Code

override func viewDidLoad()
{
    super.viewDidLoad()

     let gestureRecognizer = UIPanGestureRecognizer(target: self,
                                                   action: #selector(panGestureRecognizerHandler(_:)))
    view.addGestureRecognizer(gestureRecognizer)

}

@IBAction func panGestureRecognizerHandler(_ sender: UIPanGestureRecognizer) {
    let touchPoint = sender.location(in: view?.window)
    var initialTouchPoint = CGPoint.zero

    switch sender.state {
    case .began:
        initialTouchPoint = touchPoint

    case .changed:
        if touchPoint.y > initialTouchPoint.y {
            view.frame.origin.y = touchPoint.y - initialTouchPoint.y

        }
    case .ended, .cancelled:
        if touchPoint.y - initialTouchPoint.y > 200 {

            self.navigationController?.popViewController(animated: false)
        } else {
            UIView.animate(withDuration: 0.2, animations: {
                self.view.frame = CGRect(x: 0,
                                         y: 0,
                                         width: self.view.frame.size.width,
                                         height: self.view.frame.size.height)

            })
        }
    case .failed, .possible:
        break
    @unknown default:
        break
    }
}

Note:- My Project hierarchy like this NavigationController --> SomeViewControllers -->TabbarViewController-->ExampleViewController

enter image description here

Thanks in advance

2

There are 2 best solutions below

11
Niraj On BEST ANSWER

Tried your code, just provide the NavigationController between tab screen and your view controller.

let newVC = self.storyboard?.instantiateViewController(withIdentifier: "ExampleViewController") as! ExampleViewController
newVC.modalPresentationStyle = .overCurrentContext
self.present(newVC, animated: true, completion: nil)

And in next screen used same function as you mentioned above, no black screen.

Or you can use below line in AppDelegate as suggested here: https://stackoverflow.com/a/45994837/12830762

window?.backgroundColor = UIColor.white

5
Anil Kumar On

Insert navigation controller in between the

TabbarViewController-->ExampleViewController

So it should be :

TabbarViewController--> NavigationController -->ExampleViewController

Just Select ExampleViewController choose Embed Navigation controller in storyboard. And Use Segue push method.