You can dismiss modal viewcontroller like Facebook Messenger by pulling scrollview or navigationbar in Swift.
PullToDismiss provides dismiss modal viewcontroller function like Facebook Messenger by pulling scrollview or navigationbar with smooth and rich background effect.
sample | blur sample |
---|---|
If you update from 1.x to 2.0, see migration guide if needed.
(1) Setup PullToDismiss
import PullToDismiss
class SampleViewController: UIViewController {
@IBOutlet private weak var tableView: UITableView!
private var pullToDismiss: PullToDismiss?
override func viewDidLoad() {
super.viewDidLoad()
pullToDismiss = PullToDismiss(scrollView: tableView)
}
}
(2) Create view controller and set modalPresentationStyle
. Then present view controller
let vc = SampleViewController()
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .overCurrentContext
self.present(nav, animated: true, completion: nil)
(UIScrollView|UITableView|UICollectionView)Delegate
You can use all scroll view's delegate by set pullToDismiss.delegate
.
import PullToDismiss
class SampleViewController: UIViewController {
@IBOutlet private weak var tableView: UITableView!
private var pullToDismiss: PullToDismiss?
override func viewDidLoad() {
super.viewDidLoad()
pullToDismiss = PullToDismiss(scrollView: tableView)
pullToDismiss?.delegate = self
}
}
extension SampleViewController: UITableViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// ...
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// ...
}
}
You can customize backgroundEffect, dismissableHeightPercentage:
ShadowEffect.default
, [color: black, alpha: 0.8])pullToDismiss?.background = ShadowEffect(color: .red, alpha: 0.5) // color: red, alpha: 0.5
New feature for v1.0.
// preset blur (.extraLight, .light, .dark)
pullToDismiss?.background = BlurEffect.extraLight
// set custom Blur
pullToDismiss?.background = BlurEffect(color: .red, alpha: 0.5, blurRadius: 40.0, saturationDeltaFactor: 1.8)
// to pull half size of view controller, dismiss view controller.
pullToDismiss?.dismissableHeightPercentage = 0.5
# Swift 5.0 or later
github "sgr-ksmt/PullToDismiss" ~> 2.2
# Swift 3
github "sgr-ksmt/PullToDismiss", 2.1
carthage update
PullToDismiss is available through CocoaPods. To install it, simply add the following line to your Podfile:
# Swift 5.0 or later
pod 'PullToDismiss', '~> 2.2'
# Swift 3
pod 'PullToDismiss', '2.1'
and run pod install
Download all *.swift
files and put your project.
Change log is here.
PullToDismiss is under MIT license. See the LICENSE file for more info.