How to set cornerRadius for only top-left and top-right corner of a UIView?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to set cornerRadius for only top-left and top-right corner of a UIView?

How to Set cornerRadius for Only Top-Left and Top-Right Corner of a UIView? ✨

If you've ever wondered if there's a way to give your UIView a cornerRadius on just the top-left and top-right corners, you're in luck! 🎉 In this blog post, we'll dive into solving this specific problem, explore common issues, and provide easy solutions. So, let's get started! 💪

The Problem 💥

When you want to present a UIView with rounded corners, you might think of setting the cornerRadius property. However, by default, this applies the corner radius to all four corners. But what if you only want to round the top-left and top-right corners? 🤔

The Attempt 🔍

One possible approach is to create a CALayer and assign a shadow path with specific rounded corners using UIRectCorner. Here's an example of what that might look like:

UIView *view = [[UIView alloc] initWithFrame:frame];

CALayer *layer = [CALayer layer];
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:frame byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
layer.shadowPath = shadowPath.CGPath;
view.layer.mask = layer;

The Issue 🚧

The solution mentioned above seems promising, but one problem arises: the view disappears! 🙈 This happens because the mask property of the view's layer is set to the shadow layer, which hides the content behind it. So, how can we resolve this?

The Solution 💡

To achieve the desired corner radii without losing the view itself, we need to tweak the code a bit. Instead of assigning the shadow layer as the mask, we'll use it as a sublayer of the view's layer. Here's an updated version of the code:

UIView *view = [[UIView alloc] initWithFrame:frame];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
maskLayer.frame = view.bounds;
maskLayer.path = maskPath.CGPath;
view.layer.mask = maskLayer;

CALayer *shadowLayer = [CALayer layer];
shadowLayer.frame = view.bounds;
shadowLayer.shadowPath = maskPath.CGPath;
view.layer.insertSublayer(shadowLayer, atIndex: 0);

Now, when you run this code, you'll have the view with the desired rounded corners, and the content will be visible! 🎨

Conclusion 🌟

Setting the corner radius for only the top-left and top-right corners of a UIView may seem like a daunting task, but with the right approach, it becomes achievable and visually appealing. By utilizing CAShapeLayer and CALayer, we are able to mask and add shadow to the view without interfering with its content.

So, go ahead and start implementing this technique in your projects! Experiment with different corner radii and bring a touch of elegance to your user interfaces. And don't forget to share your creative designs with us! Use the comment section below to discuss your experiences, ask questions, or share your thoughts. We'd love to hear from you! 😄✨

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my