Call present(_:animated:) from the button's action to show the next screen, and dismiss(animated:) from that screen to close it. Since iOS 13 the default is a sheet the user can swipe down, not a full-screen cover. Set modalPresentationStyle = .fullScreen if you need the old look, use sheetPresentationController detents for a half-height sheet, and handle the swipe-down with UIAdaptivePresentationControllerDelegate.
SWIFT TIPS · UIKIT
First posted on April 5, 2018 for Swift 4, as "Hack Simple Coding [Button Action] Swift 4". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.
The 2018 note was two button actions. One presented a second screen, sliding up from the bottom, and the other dismissed it again. In 2018 that presented screen covered everything. Run the same code on a current iPhone and you get a card that leaves the previous screen peeking out at the top, and the user can swipe it away without ever touching your close button. That one change catches a lot of older code, so this rewrite is mostly about what happens around those two lines.
Present and dismiss, the full version
import UIKit
final class ListViewController: UIViewController {
private let showButton = UIButton(configuration: .filled())
override func viewDidLoad() {
super.viewDidLoad()
showButton.configuration?.title = "Show details"
showButton.addAction(UIAction { [weak self] _ in self?.showDetails() }, for: .touchUpInside)
view.addSubview(showButton)
}
// Storyboard version, same as 2018
@IBAction func showDetailsTapped(_ sender: UIButton) {
showDetails()
}
private func showDetails() {
let detail = DetailViewController()
detail.onFinish = { [weak self] in self?.reloadList() }
present(detail, animated: true)
}
func showHalfSheet() {
let detail = DetailViewController()
if let sheet = detail.sheetPresentationController {
sheet.detents = [.medium(), .large()]
sheet.prefersGrabberVisible = true
}
present(detail, animated: true)
}
func showFullScreen() {
let player = DetailViewController()
player.modalPresentationStyle = .fullScreen
present(player, animated: true)
}
private func reloadList() { print("reload") }
}
final class DetailViewController: UIViewController, UIAdaptivePresentationControllerDelegate {
var onFinish: (() -> Void)?
var hasUnsavedChanges = false {
didSet { isModalInPresentation = hasUnsavedChanges }
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
let close = UIButton(type: .close)
close.addAction(UIAction { [weak self] _ in self?.closeTapped() }, for: .touchUpInside)
view.addSubview(close)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
presentationController?.delegate = self
}
@IBAction func dismissPressed(_ sender: UIButton) {
closeTapped()
}
private func closeTapped() {
dismiss(animated: true) { [onFinish] in onFinish?() }
}
// Swipe down finished (not called when you call dismiss yourself)
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
onFinish?()
}
// Swipe down was blocked by isModalInPresentation
func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) {
let alert = UIAlertController(title: "Discard changes?", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Keep Editing", style: .cancel))
alert.addAction(UIAlertAction(title: "Discard", style: .destructive) { [weak self] _ in
self?.hasUnsavedChanges = false
self?.closeTapped()
})
present(alert, animated: true)
}
}
The two lines from 2018 are still the heart of it: present(detail, animated: true) in the list screen and dismiss(animated: true) in the detail screen. Everything else handles a question the 2018 code didn't have to ask.
Buttons without the storyboard
The storyboard @IBAction still works. For buttons made in code, iOS 14 added addAction(_:for:) with a UIAction closure, so you don't need a #selector and an @objc method anymore. UIButton(type: .close) gives you the system's standard round close button, so you don't have to draw one.
What the default presentation looks like now
A view controller's modalPresentationStyle defaults to .automatic. Apple's documentation says UIKit maps that to .formSheet in iOS 18 and later and to .pageSheet before that, and that these styles only take full effect in a regular-width window. On an iPhone in portrait, which is compact width, you see a sheet that fills most of the screen and can be swiped down.
So you have three choices, all in the code above:
- Leave the default for most detail screens. People know how to swipe a sheet away.
- Use
.fullScreenfor screens that should take over, such as a video player, a camera or onboarding. - Use detents for a half-height sheet.
sheetPresentationControllercame in iOS 15. With[.medium(), .large()]the sheet opens at half height and the user can drag it up, andprefersGrabberVisibleshows the small handle that hints at this.
When the user swipes the sheet away
This is the part that bites. If the list needs to reload after the detail screen closes, calling reloadList() only from the close button misses every swipe-down. The detail screen above handles both paths:
- The close button calls
dismissand runsonFinishin its completion handler. - A swipe-down calls
presentationControllerDidDismiss(_:)on the presentation controller's delegate. Apple's documentation says this method is not called when the screen is dismissed in code, so the two paths never both run.
Don't rely on viewWillAppear in the list screen for this. With a sheet, the list screen never left the window, so UIKit has no reason to tell it that it's appearing again. I didn't measure that on a device for this rewrite, but it's why the code passes a closure instead.
Stopping the swipe when there are unsaved changes
If the detail screen is a form, a stray swipe shouldn't throw away what someone typed. Setting isModalInPresentation to true blocks interactive dismissal. The code ties it to hasUnsavedChanges, so the sheet is only locked while there is something to lose. When the user tries to swipe anyway, UIKit calls presentationControllerDidAttemptToDismiss(_:), and Apple's documentation suggests explaining the situation there, which the code does with a Keep Editing or Discard alert.
A quick check for your project
- Search for
present(and list the screens that reload data or save something when they close. Each one needs to handle the swipe-down too. - Search for
viewWillAppearin screens that present sheets. Code there that expects to run after a sheet closes probably doesn't. - Open each form in a sheet, type something and swipe down. If your text disappears without a question, add
isModalInPresentation.
What the 2018 version said
The original post had a "Pressed" action with the present call commented out, and a "dismissPressed" action that called dismiss(animated: true, completion: nil). Both still compile. The rest of this post is what changed around them.
Keep reading on JR Devhub
Comments