swiftuiviewcontrolleruimodalpresentationstyle

How to present ViewController in full screen with behavior of popover? Swift


I need to present VC in full screen, but with ability to swipe it down as it was popover, similar to how it works in Apple Music in song details.

I've tried every option in vc.modalPresentationStyle, but it's either in full screen without ability to swipe down to close or with this ability, but not in full screen

    vc.modalPresentationStyle = .overFullScreen
    vc.modalTransitionStyle = .coverVertical

Apple Music reference


Solution

  • So, I just presented it in full screen and wrote function, which allows pan to close vc, with decreasing alpha

    func panToCloseGesture() {
            let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
            view.addGestureRecognizer(panGesture)
        }
        
        @objc func handlePanGesture(_ sender: UIPanGestureRecognizer) {
            let percentThreshold:CGFloat = 0.3
            let translation = sender.translation(in: view)
            
            let newY = self.ensureRange(value: view.frame.minY + translation.y, minimum: 0, maximum: view.frame.maxY)
            let progress = self.progressAlongAxis(newY, view.bounds.height)
            
            view.frame.origin.y = newY
            if newY > 0 {
                self.rootView.alpha = 1 - (newY / 400)
            }
            
             if sender.state == .ended {
                let velocity = sender.velocity(in: view)
                if velocity.y >= 1000 || progress > percentThreshold {
                    self.dismiss(animated: true, completion: nil)
                } else {
                    UIView.animate(withDuration: 0.2, animations: {
                        self.rootView.alpha = 1
                        self.view.frame.origin.y = 0
                    })
                }
            }
            sender.setTranslation(.zero, in: view)
        }