How to create a Popup Window in iOS using Swift

How to create a Popup Window in iOS using Swift

Today, I’ll show you how to create a popup window and how easily you can use it inside your app.

Creating the Popup Window View

Create a new swift file by right clicking on your project name > New File…

Choose Swift File and press Next

…give the name PopUpWindow and press Create:

Inside the PopUpWindow.swift file, create a UIView private class with a name PopUpWindowView

This UIView will be the semi-transparent black background for our popup window.

In the init(), create an UIView for the window, two UILabel for the title and the message, and an UIButton that close the popup when you press it.

In the same .swift file, create an UIViewController class with a name PopUpWindow

Declare a variable of PopUpWindowView() and pass the data from UIViewController‘s init to the popup view.

Close the window when you press the button…

…and add the popup window UIView to the UIViewController:

Using the Popup Window

Now, every time we want to show the popup window, the only thing we have to do is:

You can find the final project here

If you have any questions feel free to DM me on Twitter @johncodeos or leave a comment below!

Leave a Reply

Notify of

Follow me