Define Machine Learning

The term Machine Learning was coined by Arthur Samuel in 1959, an American pioneer in the field of computer gaming and artificial intelligence and stated that “it gives computers the ability to learn…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




A Protocol oriented approach for Rounded UINavigationBar

Despite having been around for decades, from the past few years there’s a huge jump in user experience & user interface design. Apple with every release gives some of the fine updates to make the UX/UI experience crystal to its users. I personally like iOS because of it’s simplicity & clean design.

And as a developer we should keep our apps up-front with the latest design changes happening in tech world. An app with awesome UX/UI is what attracts users most, let it be a small UIButton or as simple as a Font weight every single component matters!

Some of the components provided by apple are excellent but there’s always room for more, That’s when custom components comes in spotlight.

I’m going to make a series of such refinements in swift and see its wonders.

Today I’m going to talk about UINavigation Bar, a simple navigation bar with rounded corners. There are lots of tutorials about this, and there’s no particular reason why another one is needed. However, while often seeing them, I thought why not give it a shot and study how it is done with a different view point ie POP, yes Protocol Oriented Programming. I’ve arrived at my own way of understanding it, so perhaps this article will help someone out there to see how this is done.

First thing first, let me show you what you are going to get at the end of this article:

We are going to make our navigation bar look like this. And not just that , we are going to achieve it through Protocol Oriented Programming. Oh Yeah!
But like all professions, it’s impossible to distill the process from just a few words so let’s jump to code.

Create a simple project with a single view:

Now lets’ add a UIButton at the centre of main view which will help us to navigate on next screen. I’m doing this programmatically, but one can do whichever way one likes.

A basic setup of UIButton and added on UiViewController’s view

After doing some basic setup of the button, let’s now create another ViewController and open it as a UINavigationController.

After this, add a title to new view controller’s navigation bar like:

when we run our app and tap on the Show View button we will see the default Navigation bar with title. So far everything looks normal its time to write the protocol we are waiting for.

Here, we have wrote a protocol with a addRoundedCorner function in it, which will take the UINavigationBar as a parameter which needs to be worked upon and also the desired radius.
It’s not just this, but we can do lot more from swift’s protocol, we are now going to restrict the use of this protocol only to UIViewController, this could be achieved by extension, let me show you how:

Just like that! Now the RoundedCornerNavigationBar will only work for UIViewController, swift does all the awesome work behind.

Coming back to view, first we will set the translucent property of the UINavigationBar to false, Since we will remove the default background image.

Now we will add a custom UIView which will going to deal with the rounded shape, create a UIView and place it’s y position to the max of the UINavigationBar’s. Keeping its colour .clear and then we will insert it at UINavigationBar’s subview.

Now, we’ll need to create a CAShapeLayer object which will have a shadow and curves, when it comes to curves and shaped I prefers UIBezierPath

(Tip: Read more about UIBezierPath, its fun :))

Since we need bottom left and right corner to be round ergo we have sent only those in the initialiser but you can play around it for your custom views, This will returns us an immutable CGPathRef which is only valid until the UIBezierPath is further mutated. Setting the path will create an immutable copy of the provided CGPathRef, so any further mutations on a provided CGMutablePathRef will be ignored.
After getting the path, we now just have to setup colour and shadows.

Our complete protocol code now will look like this:

Boom! It’s done, our protocol is ready to be use. Let’s jump straight to the UIViewController where we want to use it. First we need to make our UIViewController to access the RoundedCornerNavigationBar protocol which could be done like this:

Now in viewWillAppear access our protocol’s method ie

and send the self.navigationController’s navigationBar and corner radius.
That’s it! Run the app and see the results…

To conclude:
We now have a protocol which could make any UIViewController navigation Bar rounded. Awesome! Right?

Thanks for your time. Feel free to send me your feedbacks. Corrections and suggestions are greatly appreciated. To find the complete source code here the link of the GitHub project.

Add a comment

Related posts:

Anxiety in the Age of Coronavirus

If you have a generalised anxiety disorder you might often feel like you’re alone when anxiety rears its head in your daily life. But whether you’re already no stranger to anxiety, or if you’re…

A new solution to an old problem. Cancer treatment with CBD

According to statistics, every two minutes, someone in the UK hears the devastating diagnosis — cancer. Two minutes. It’s not enough to drink your espresso or eat the morning sandwich. Two minutes…

Why Overdelivering Is a Terrible Habit For Millennial Women

I love working with high-achievers like you because of your desire to add value and make a difference every single day. But chronic over-delivery holds you back in your career. It keeps you in the…