How to make UIView Clickable in Swift — Let Create An App

Shubham Agarwal
3 min readMar 12, 2022

UIView is the core element in swift, we always use UIView if we want to create something unique like a custom design, a custom button. It is an object which used to manage rectangle shape on the screen. But we have one limitation with the UIView, we can not get action for the UIView. To make UIView clickable we need to put UITapGesture on that view. But is there any other way to do this without any coding, the answer is YES!

In this video, we are going to learn how we can make UIView clickable in Swift without using UITapGesture, and without doing any coding to make that view tappable.

You can also read the article about Quiz App in Swift to learn more and understand how it helps in the real problem.

If you want to watch the video tutorial of this article, here you go:

How to make UIView Clickable in Swift

1. Create a New Swift Project

Open Xcode, Select App and click on next and give a proper name to the project and make sure to select the user interface should be Swift, and create the project.

2. Create User Interface

We need to design some sample button-style views on the screen. So we can understand how it is useful to make UIView clickable directly without using UITapGesture.

So let’s quickly design some buttons on the screen, You can design these buttons as your need or can use this functionality directly on the view.

The above image shows some sample button styles that we face in day-to-day life, but for creating these kinds of buttons we use to view and put tap gestures on them. Now let’s see how we can create a view clickable with a tap gesture.

3. Create User Interface

For creating action of the above button first assign UIControl class to every view in the storyboard.

Now, Let’s create the action of view by dragging on the view controller.

Now click on the connect button and it will create the action on the ViewController.

Now, create the action for all the views in the same way. Now let’s run the application and see the debug area.

As you can clearly see in the output window when I clicked on any of the views. It will show the print statement in the output window.

Now you can run the application and test the complete functionality.

Download Resources for How to make UIView Clickable in Swift

Download the source code of How to make UIView Clickable in Swift.

I hope you like this tutorial and if you want any help let me know in the comment section.

Stay tuned, there is way more to come! follow me on Youtube, Instagram, Twitter. So you don’t miss out on all future Articles and Video tutorials.

. . .

I am delighted that you read my article! If you have any suggestions do let me know! I’d love to hear from you. ????

Originally published at https://letcreateanapp.com on March 12, 2022.

--

--