0

I am presenting a layover VC programmatically.

However, I would like to keep the swipe down functionality of dismissing the VC and keep the background blur while ultimately presenting the VC half way from the bottom of view.

Presenting VC:

let navVC = UINavigationController(rootViewController: SnackBarViewController())
    
present(navVC, animated: true)

I have found a way to present the VC half way like so, but I lose the swipe functionality and the background blur.

let navVC = UINavigationController(rootViewController: SnackBarViewController())
navVC.transitioningDelegate = self
navVC.modalPresentationStyle = .custom
present(navVC, animated: true)

extension SettingsViewController : UIViewControllerTransitioningDelegate {
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        return HalfSizePresentationController(presentedViewController: presented, presenting: presenting)
    }
}


class HalfSizePresentationController : UIPresentationController {
    override var frameOfPresentedViewInContainerView: CGRect {
        get {
            guard let theView = containerView else {
                return CGRect.zero
            }

            return CGRect(x: 0, y: theView.bounds.height/2, width: theView.bounds.width, height: theView.bounds.height/2)
        }
    }
}

Is there a better way to present the VC half way in swift 5 without losing the presentation style format by default?

I have also heard of UISheetPresentationController using detents but im not entirely sure how to implement.

Reference here

enter image description here

base ios version 13.0. xcode version 12.5.1

CoderChick
  • 192
  • 4
  • 16
  • Detents is pretty simple: see https://stackoverflow.com/a/67988976/14351818 – aheze Jun 23 '21 at 21:37
  • Note that it's only for iOS 15 and above though – aheze Jun 23 '21 at 21:37
  • So is there a better way to present the vc with default functionality half way? – CoderChick Jun 23 '21 at 21:38
  • Well `UISheetPresentationController`/`detents` is definitely the easiest and most polished, but there are libraries like [PanModal](https://github.com/slackhq/PanModal) that you can use – aheze Jun 23 '21 at 21:42
  • It's not about Swift 5 it's about UIKit version. You should communicate the Xcode version instead which is the faster way to know what is the base iOS SDK version and UIKit as well. – Blazej SLEBODA Jun 23 '21 at 21:54
  • base ios version 13.0. xcode version 12.5.1 – CoderChick Jun 23 '21 at 22:22

0 Answers0