There are quite a few modal sheets used when creating apps.
Therefore, I would like to use the library so that it can be easily implemented.
This time, we will implement it using PanModal
.
Click here for more details ↓ PanModal
Create a podfile and write the following
pod 'PanModal'
First, let's import PanModal
import UIKit
import PanModal
The class to be displayed modally must have PanModalPresentable
applied.
The class to be displayed modally this time is ModalViewController
.
ModalViewController.swift
class ModalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBlue
}
}
ModalViewController.swift
extension ModalViewController: PanModalPresentable {
var panScrollable: UIScrollView? {
return nil
}
//Set the modal height to 200 from the bottom of the screen(Default height when displaying modals)
var shortFormHeight: PanModalHeight {
return .contentHeight(200)
}
//Limit modal height to a maximum of 400 from the top of the screen
//If you don't set this value, the modal can swipe to the top of the screen.
var longFormHeight: PanModalHeight {
return .maxHeightWithTopInset(400)
}
//Modal background color
var panModalBackgroundColor: UIColor {
return UIColor.black.withAlphaComponent(0.2)
}
//Whether you can swipe up(True by default)
var anchorModalToLongForm: Bool {
return false
}
//Whether to round the top corners of the modal
var shouldRoundTopCorners: Bool {
return true
}
//Whether to display something like the home bar of a smartphone without a home button
var showDragIndicator: Bool {
return true
}
//Whether the user can operate the displayed modal(If set to false, it cannot be operated.)
var isUserInteractionEnabled: Bool {
return false
}
}
ViewController.swift
class ViewController: UIViewController {
let button: UIButton = {
let button = UIButton()
button.setTitle("display!", for: .normal)
button.titleLabel?.font = .boldSystemFont(ofSize: 20)
button.layer.cornerRadius = 20
button.backgroundColor = .orange
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
button.center = view.center
button.addTarget(self, action: #selector(presentModal(_:)), for: .touchUpInside)
view.addSubview(button)
}
@objc func presentModal(_ sender: UIButton) {
presentPanModal(ModalViewController())
}
}
To display a modal, you must put the ModalViewController
created for the modal in presentPanModal
and call it.
In this example, presentPanModal
is called by adding a process to display a modal to the button.
When I think of Pan, I think of a pianist, but I think I could write it in a fairly easy-to-understand manner. The library is convenient so let's all use it! Thank you for reading.
Recommended Posts