[Swift] Easy to implement modal with PanModal

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

Introducing the library

Create a podfile and write the following

pod 'PanModal'

Make something to display

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
    }
}

Make something to display

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.

Display result

Finally

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

[Swift] Easy to implement modal with PanModal
How to implement UICollectionView in Swift with code only
Try to implement login function with Spring-Boot
[Swift] How to implement the countdown function
How to implement TextInputLayout with validation function
[Swift5] How to implement standby screen using'PKHUD'
[Swift5] How to implement animation using "lottie-ios"
Easy to trip with Java regular expressions
I tried to implement ModanShogi with Kinx
Swift beginners tried to implement microwave logic!
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
[Swift] How to link the app with Firebase
Try to implement login function with Spring Boot
Transition to a view controller with Swift WebKit
Super easy way to use enum with JSP
Swift beginners tried to implement vending machine logic!
[Swift] How to implement the fade-in / out function
[Rails] How to easily implement numbers with pull-down
Try to implement TCP / IP + NIO with JAVA
Easy to make LINE BOT with Java Servlet
Easy to display hello world with Rails + Docker
Add packages to your project with Swift PM
[Swift] I tried to implement Instagram profile-like UI with UICollectionView only with code without storyboard
I tried to implement file upload with Spring MVC
I tried to implement TCP / IP + BIO with JAVA
Try to implement using Rakuten product search API (easy)
I tried to implement Stalin sort with Java Collector
[Swift] Implement swipe processing
Getting Started with Swift
Starting with Swift Swift UI
Easy to maintain FizzBuzz
[Swift] I tried to implement exception handling for vending machines
Add a local Swift Package to your project with Swift PM
Port C code with a lot of typecasts to Swift
[Swift] I tried to implement the function of the vending machine
[Easy] How to automatically format Ruby erb file with vsCode
How to implement infinite scrolling (page nate) in Swift TableView
[Rails] I tried to implement batch processing with Rake task
[swift5] Try to make an API client with various methods
Implement writing and reading to Property List (.plist) in Swift
I want to implement various functions with kotlin and java!
A story about using the CoreImage framework to erase stains with Swift and implement a blur erase function