Sitemap
Better Programming

Advice for programmers.

Press enter or click to view image in full size

How do you present the standard iOS modal view with SwiftUI?

3 min readSep 28, 2019

--

SwiftUI is a new framework that makes app development lightning fast. It was introduced by Apple this June at WWDC 2019.

I’m pretty sure this will be changed in the newer versions of the SwiftUI, but for now, here is an open-source package for everyone to present modal views conveniently. The library can be added as a Swift package to a project and benefit from updates, or just copy the file if the functionality covers all the edge cases for your project already.

Step 1: Add the ModalView Library as a Swift Package in Xcode

Press enter or click to view image in full size
Adding SwiftUI modal view to xcode project using Swift Package Manager (Swift PM): File -> Swift Packages -> Add Package De..
Adding SwiftUI modal view using Swift Package Manager

Here is the link: https://github.com/diniska/modal-view

In the library, we use an analogy with the existing navigation API:

  • ModalPresenter is similar toNavigationView
  • ModalLink is an analogue of NavigationLink

Thus, the modal API will be familiar to everyone who has already dealt with navigation.

Step 2: Create ModalPresenter similarly to NavigationView

Let’s add ModalPresenter somewhere close to the root of our view.

It doesn’t have to be the root — just make sure it’s not inside a List, Form, or a similar view that creates subviews dynamically.

Step 3: Add ModalLink similarly to NavigationLink

We are wrapping Text that wants to trigger the modal view with NavigationLink.

Here you go:

Displaying modal view on iPad using SwiftUI
SwiftUI modal view with ModalPresenter and ModalLink

In complex hierarchies, like navigation view with a form with buttons inside, is the place where ModalPresenter view shines comparing to the default API.

Press enter or click to view image in full size
How to display modal view from List and Form with Swift UI iOS 13
Displaying modal views from Form view in Swift UI
Implementation of complex hierarchy with NavigationView, Form, and Sections

To put it short, that is pretty convenient. Now let’s check how we would implement that without the library and see what hidden results for List and Form views it will give us.

How Would You Display a Modal View Without Using the ModalView Library?

The default method is to use the sheet API. A sheet requires a binding to a Boolean which controls whether the dialogue is shown or hidden.

Also, don’t forget about toggling the state on button tap. In a simple case, the result would look like this.

Pretty good, huh? The hard part is in the list of items. Let’s try that:

You’ve probably got the idea.

The code is longer and less safe, as it is easy to write isPresented5 in one place and isPresented6 in the other just because of copy-pasting.

This is not a big issue, though. A big issue is that every button here works only once. Don’t ask me why.

Press enter or click to view image in full size
Buttons in the Form works only one time with SwiftUI sheet method
Using SwiftUI sheet and Form. Buttons work only first time.

To conclude, personally, I’ll go with the new ModalView library in my projects for now as this framework provides a bit simpler API and makes the code-writing experience even faster.

If you are aware of other edge cases, pull requests and issues are welcome in the package repo.

Enjoy your SwiftUI experience — the world can’t wait to see your new apps.

If you found this article beneficial, my Medium blog. It costs nothing for you but is immensely for me, as it helps me continue to share valuable content with readers like you. Thank you for your support!

--

--

Denis Chaschin
Denis Chaschin

Written by Denis Chaschin

Creating iOS apps. Living in Sydney, originally from Saint-Petersburg