How to create custom UIView Shapes in iOS Swift 2.1

Today we will learn How to create custom UIView Shapes in iOS Swift 2.1. As sometimes we need to create custom shapes according to App’s requirement.

We will start by creating a new project and Choosing language Swift, i’m using Xcode 7.2 version.

So create New project name it CustomShapes and save it.

Now in Main.storyboard drag drop a UIView on controller and change its Background Color to Blue.

 

How to Create custom view in Swift2.1 ios
How to Create custom view in Swift2.1 iOS

After then add Constraints to your view.

As we are creating custom view so we will create a new sub class of UIView. Name is “Shape”. Add new file > cocoa touch class > sub class of UIView name Shape. Then save it.

How to Create custom view in Swift2.1 ios
How to Create custom view in Swift2.1 ios

We will only override the “drawRect” method as it is also written in the newly build sub class of UIView.

Let Start customising view.

First we create a layer and then we will customise it accordingly .

 

After that we will create a path its like out lining the shape of the view and later applying it on view.

 

 

After that we will apply that path on the view.

 

That all the coding part we have to do in this class now run the programme.

There will be a blue rectangle on the screen we forgot one thing to select the view and change its class to “Shape”.

Now again run the project you will see the shape we just created.

 

How to Create custom view in Swift2.1 ios
How to Create custom view in Swift2.1 ios

You finally create your own custom view, you can change your shape.

Another news for you all now you can download the full project. You can download this project from here.

Today we learn How to create custom UIView Shapes in iOS Swift 2.1. Next time we will do some more animations till then Happy Coding 🙂

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.