modal segue
view controller
iOS development
user interface
Swift programming

Closing a view displayed via a modal segue

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In iOS app development, modals are a popular way to present new content temporarily without navigating away from the current screen. One common use case for modals is disclosing detailed views, which users can dismiss when they finish interacting. This article focuses on closing a view displayed via a modal segue in an iOS application, providing technical explanations and examples to guide developers.

What is a Modal Segue?

When a view controller presents another view controller modally, it overlays the new content over the existing screen. The user must explicitly dismiss this modal view to return to the previous context. Modals are often used for tasks like user authentication, signing up, or filling out forms.

How Modal Segues Work

In the storyboard environment in Xcode, a modal segue can be set up by Control-dragging from one view controller to another and selecting `present modally`. This connection tells iOS to instantiate and display the target view controller using a full-screen or partial-screen overlay.

iOS Modal Presentation Styles

In modern iOS development, several presentation styles are available for modals, including:

  • Full Screen: The new view controller covers the entire screen.
  • Page Sheet: Partially covers the screen, often used on iPads.
  • Form Sheet: Smaller window centered on the screen.
  • Popover: A floating box anchored to a specific location from the existing view.

Each style influences how users can interact with the previous view and how the view should be dismissed.

Closing a Modal View: Technical Guide

Utilizing `dismiss(animated:completion:)`

To dismiss a modal-view controller, you typically use the `dismiss(animated:completion:)` method. This function gently fades out the view, restoring the previous screen.

Example:

  • Control-drag from your dismiss button to the `Exit icon` and choose your unwind action.
  • Interruptibility: Allow users to cancel or back out of modal interactions gracefully.
  • Focus: Use modals for focused tasks that shouldn't interrupt the user's workflow.

Course illustration
Course illustration

All Rights Reserved.